from sqlalchemy import create_engine, text dsn = "mssql+pyodbc://sa:Mffu3011#@localhost\\GLOBALCUBE/P2?driver=ODBC+Driver+17+for+SQL+Server" base_dir = "C:\\Projekte\\P2\\views\\" engine = create_engine(dsn) with engine.connect() as conn: result = conn.execute(text("SELECT * FROM import.SYSVIEWS where vcreator = 'bungert'")) for row in result: with open(base_dir + row.viewname + ".sql", "w", encoding="utf-8") as fwh: fwh.write(row.viewtext)