python同时执行5个sql_如何在Python脚本中执行多个SQL语句?

我正在使用Python中的以下代码模板(使用Atom构建/编写)。在import pyodbc

import pandas as pd

import win32com.client

cnxn = pyodbc.connect('Trusted_Connection=yes', driver = '{SQL

Server}',server ='prodserver', database = 'XXXX')

cnxn.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')

cnxn.setencoding(str, encoding='utf-8')

cnxn.setencoding(unicode, encoding='utf-8', ctype=pyodbc.SQL_CHAR)

cursor = cnxn.cursor()

script ="""SELECT AccountsCount.AccountClass, COUNT(*) as Count

FROM

(SELECT *

FROM XXXX.dbo.table

where SubNo='001'

AND (DATENAME(WEEKDAY, GETDATE()) = 'Sunday' AND

convert(date,AddDate) = DATEADD(DAY, -2, CAST(GETDATE() as DATE))

) OR

(DATENAME(WEEKDAY, GETDATE()) = 'Monday' AND

convert(date,AddDate) = DATEADD(DAY, -3, CAST(GETDATE() as DATE))

) OR

(DATENAME(WEEKDAY, GETDATE()) = 'Sunday' AND

convert(date,AddDate) = DATEADD(DAY, -2, CAST(GETDATE() as DATE))

) OR

(DATENAME(WEEKDAY, GETDATE()) NOT IN ('Sunday', 'Monday') AND

convert(date,AddDate) = DATEADD(DAY, -1, CAST(GETDATE() as DATE))

)) AS AccountsCount

Group by AccountsCount.AccountClass

"""

df = pd.read_sql_query(script,cnxn)

writer = pd.ExcelWriter ('ExcelFile.xlsx')

df.to_excel(writer, sheet_name = 'Data Export')

writer.save()

xlApp = win32com.client.DispatchEx('Excel.Application')

xlsPath = ('OtherExcelFile.xlsm')

wb = xlApp.Workbooks.Open(Filename=xlsPath)

xlApp.Run('CopyIntoOutlook')

wb.Save()

xlApp.Quit()

我需要做的就是在这个脚本中添加第二个完全独立的SQL命令,它可以完美地运行,并按照上面所述完成我需要的操作。我的附加脚本是这样的

^{pr2}$

我没有任何运气,我已经尝试加入脚本,任何帮助是非常感谢!您会注意到第二个脚本使用了与原始脚本相同的数据库和表,我只需要YTD数据以及前一天查看的top查询。在

你可能感兴趣的:(python同时执行5个sql)