Python-T-SQL语句在SQL Server中工作,但当我在Python中使用它时,它总是出错

Python-T-SQL语句在SQL Server中工作,但当我在Python中使用它时,它总是出错,python,sql,sql-server,Python,Sql,Sql Server,我能够通过Python连接并执行SQL Server数据库的基本查询,但一旦我开始向查询添加内部连接s等,Python就会出错: 关键字“Order”附近的语法不正确。DB Lib错误消息20018,严重性15:\n一般SQL Server错误:检查来自SQL Server的消息\n 我将把代码放在下面,但我认为一定有一些Python格式我弄错了,因为查询使用其他SQL工具工作 在我的搜索中,我看到在Python中使用T-SQL时,内部联接应该缩进ON部分。由于我有太多的内部联接s,可能是缩进不

我能够通过Python连接并执行SQL Server数据库的基本查询,但一旦我开始向查询添加
内部连接
s等,Python就会出错:

关键字“Order”附近的语法不正确。DB Lib错误消息20018,严重性15:\n一般SQL Server错误:检查来自SQL Server的消息\n

我将把代码放在下面,但我认为一定有一些Python格式我弄错了,因为查询使用其他SQL工具工作

在我的搜索中,我看到在Python中使用T-SQL时,
内部联接
应该缩进
ON
部分。由于我有太多的
内部联接
s,可能是缩进不正确?我还看到,在Python中分解SQL时,必须在每行末尾加一个\

任何帮助或链接都将不胜感激

    import pymssql

    conn = pymssql.connect(server= 'xxx',
                           user= 'xxx',
                           password= 'xxx',
                           database= 'xxx'
                           )

    cursor = conn.cursor()

    sql = "SELECT PatientInfo.MRN, AccountPersonalInfo.LastName, Visit.VisitNumber, PatientInfo.FirstName, PatientInfo.LastName, AccountPersonalInfo.FirstName, Report.LastSignDate, Order.ProcedureDescList, Visit.Facility, Order.CompleteDate, Order.FillerOrderNumber \
FROM ((Comm4.dbo.Order Order  INNER JOIN Comm4.dbo.Report Report \
                                  ON Order.ReportID=Report.ReportID) \
      INNER JOIN (Comm4.dbo.PatientInfo PatientInfo INNER JOIN Comm4.dbo.Visit Visit \
                                                        ON PatientInfo.PatientID=Visit.PatientID) \
          ON Order.VisitID=Visit.VisitID) INNER JOIN Comm4.dbo.AccountPersonalInfo AccountPersonalInfo \
                                              ON Report.SignerAcctID=AccountPersonalInfo.AccountID \
WHERE  PatientInfo.MRN<>'TEMPORARY' AND Report.LastSignDate>={ts '2020-09-01 00:00:00'} AND Report.LastSignDate<{ts '2020-10-01 00:00:00'}) \
ORDER BY Report.LastSignDate, PatientInfo.MRN"

     cursor.execute(sql)

     row = cursor.fetchone()

     conn.close()

     print(row) 
     
导入pymssql
conn=PymSQL.connect(服务器='xxx',
用户='xxx',
密码='xxx',
数据库='xxx'
)
游标=连接游标()
sql=“选择PatientInfo.MRN、AccountPersonalInfo.LastName、Visit.VisitNumber、PatientInfo.FirstName、PatientInfo.LastName、AccountPersonalInfo.FirstName、Report.LastSignDate、Order.proceduredeslist、Visit.Facility、Order.CompleteDate、Order.filleroorderNumber\
FROM((Comm4.dbo.Order Order)内部联接Comm4.dbo.Report报告\
ON Order.ReportID=Report.ReportID)\
内部连接(Comm4.dbo.PatientInfo PatientInfo内部连接Comm4.dbo.Visit访问\
ON PatientInfo.PatientID=Visit.PatientID)\
ON Order.VisitID=Visit.VisitID)内部连接Comm4.dbo.AccountPersonalInfo AccountPersonalInfo\
在Report.SignerAcctID=AccountPersonalInfo.AccountID上\

其中PatientInfo.MRN'TEMPORARY'和Report.LastSignDate>={ts'2020-09-01 00:00:00'}和Report.LastSignDate您的sql语法有几个错误,不需要额外的括号“((执行联接时。您不必担心SQL语句的缩进,但是python缩进和换行可能有点棘手。为了简化代码,您可以在python中使用多行字符串(即使用
“some string”“”

导入pymssql
conn=pymssql.connect(服务器='xxx',
用户='xxx',
密码='xxx',
数据库='xxx'
)
游标=连接游标()
sql=”“”
挑选
PatientInfo.MRN,
AccountPersonalInfo.LastName,
Visit.VisitNumber,
PatientInfo.FirstName,
PatientInfo.LastName,
AccountPersonalInfo.FirstName,
Report.LastSignDate,
订单。程序清单,
参观.设施,,
订单,完成日期,
Orders.FillerOrderNumber
从…起
Comm4.dbo.Order命令
内连接
Comm4.dbo.Report订单报告。ReportID=Report.ReportID
内连接
Comm4.dbo.Visit-ON-Orders.VisitID=Visit.VisitID
内连接
Comm4.dbo.PatientInfo PatientInfo ON PatientInfo.PatientID=Visit.PatientID
内连接
Comm4.dbo.AccountPersonalInfo AccountPersonalInfo ON
Report.SignerAcctID=AccountPersonalInfo.AccountID
哪里
PatientInfo.MRN“临时”和
Report.LastSignDate>={ts'2020-09-01 00:00:00}和

Report.LastSignDate因为
Order
是一个,您需要用方括号转义标识符:
[Order]
。否则编译器会认为您试图调用
Order BY
命令。最好使用表别名以避免重复较长的表名:

sql=“”选择pi.mrn
,a.姓
,v.visitnumber
,pi.firstname
,pi.lastname
,a.名字
,r.lastsigndate
,o.proceduredesclist
,v.设施
,o.completedate
,o.fillerordernumber
从…起
(
(通讯4.dbo.[命令]o
内部联接comm4.dbo.r报告
在o.reportid=r.reportid上)
内连接
(comm4.dbo.patientinfo pi
内部连接通信4.dbo.visit v
关于pi.patientid=v.patientid)
在o.visitid=v.visitid上
) 
内部连接comm4.dbo.accountpersonalinfo a
在r.signeracctid=a.accountid上
其中pi.mrn“临时”
和r.lastsigndate>={ts'2020-09-01 00:00:00'}
和r.lastsigndate<{ts'2020-10-01 00:00:00'}
r.lastsigndate订购
,pi.mrn
"""
虽然可以使用带括号的嵌套联接(类似于MS Access SQL),但可以避免这种嵌套,因为所有联接都是
内部的
。更平坦的SQL语句可以提高可读性和可维护性

sql=“”选择pi.mrn
,a.姓
,v.visitnumber
,pi.firstname
,pi.lastname
,a.名字
,r.lastsigndate
,o.proceduredesclist
,v.设施
,o.completedate
,o.fillerordernumber
来自comm4.dbo.[ORDER]o
内部联接comm4.dbo.r报告
在o.reportid=r.reportid上
内部连接通信4.dbo.visit v
在o.visitid=v.visitid上
内部连接comm4.dbo.TINFO pi
关于pi.patientid=v.patientid
内部连接comm4.dbo.accountpersonalinfo a
在r.signeracctid=a.accountid上
其中pi.mrn“临时”
和r.lastsigndate>={ts'2020-09-01 00:00:00'}
和r.lastsigndate<{ts'2020-10-01