首页 > 解决方案 > Python - T-SQL 语句在 SQL Server 中工作,但是当我在 Python 中使用它时,它一直出错

问题描述

我可以通过 Python 连接并执行 SQL Server 数据库的基本查询,但是一旦我开始将INNER JOINs 等添加到查询 Python 错误中:

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

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

在我的搜索中,我看到在 Python 中使用 T-SQL 时INNER JOIN应该缩进该部分。ON既然我有这么多INNER JOINs,也许我缩进不正确?我还看到,当您在 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) 
     

标签: pythonsqlsql-server

解决方案


您的 sql 语法中有几个错误,执行连接时不需要额外的括号“((”)。您不必担心缩进 SQL 语句,但是 python 缩进和换行可能有点棘手。为了简化代码,您可以""" some string """在python中利用多行字符串(即使用),例如。

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, 
    Orders.ProcedureDescList, 
    Visit.Facility, 
    Orders.CompleteDate, 
    Orders.FillerOrderNumber 
FROM 
    Comm4.dbo.Order Orders  
INNER JOIN 
    Comm4.dbo.Report Report ON Orders.ReportID=Report.ReportID
INNER JOIN 
    Comm4.dbo.Visit Visit ON Orders.VisitID=Visit.VisitID
INNER JOIN 
    Comm4.dbo.PatientInfo PatientInfo ON PatientInfo.PatientID=Visit.PatientID
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) 

推荐阅读