Here is how we get data back out of the database and write it to a file if the amount of data you want is relatively small. This process should not take very long. If it does, you want QueryDatabaseAndWriteLargeFile.
import sysimport pandas as pdimport pyodbc as db#Constants used to process data files#Exsiting paths are examples only!kFileDestination ='C:/InterfaceAndExtractFiles/../Out/data.csv'#full path to processing Out folder#small amounts of dataprint("Starting: Processing Data")#alter the below for your file.try:print("Connecting to SQL Server database") connection_string ='DSN=ETL;' conn = db.connect(connection_string) csr = conn.cursor() sql ="SELECT * FROM [table or view]" df = pd.read_sql(sql,conn) conn.commit() csr.close() conn.close()exceptExceptionas e:print(e)#Write Filedf.to_csv(kFileDestination, sep="|",index=False)# Script Completeprint("Complete: Processing Data")