我需要创建一些临时表并将其联接并从中检索数据。我无法使用sqlalchemy
创建临时表,因为它会出现编程错误。
我尝试了这个Python Running SQL Query With Temp Tables
sqlengine = sqlalchemy.create_engine('mssql+pyodbc://user:pass@localhost/db?driver=SQL+Server+Native+Client+11.0')
filename = 'C:\\Users\\Desktop\\sql_test.txt'
file = open(filename, 'r')
text = file.read()
file.close()
sql = text.split('/**/')
com1 = sql[0]
com2 = sql[1]
sqlengine.execute(com1)
sql_test.txt
包含以下简单代码
select top 10 *
from notification
into #table1
/**/
select * from #table1
结果为:
ProgrammingError:(pyodbc.ProgrammingError)('42000',“ [42000] [Microsoft] [SQL Server Native Client 11.0] [SQL Server]关键字'into'附近的语法不正确。(156)(SQLExecDirectW)”) [SQL:选择前10名* 从通知 进入#table1]