python - Python - T-SQL 语句在 SQL Server 中工作,但是当我在 Python 中使用它时,它一直出错
问题描述
我可以通过 Python 连接并执行 SQL Server 数据库的基本查询,但是一旦我开始将INNER JOIN
s 等添加到查询 Python 错误中:
关键字 'Order' 附近的语法不正确。DB-Lib 错误消息 20018,严重性 15:\n一般 SQL Server 错误: 检查来自 SQL Server 的消息\n
我将把代码放在下面,但我认为一定有一些 Python 格式我弄错了,因为查询使用其他 SQL 工具工作。
在我的搜索中,我看到在 Python 中使用 T-SQL 时INNER JOIN
应该缩进该部分。ON
既然我有这么多INNER JOIN
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)
解决方案
您的 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)
推荐阅读
- flutter - Flutter 从自动完成获取城市,国家
- arangodb - How to filter documents in a collection when inside an array in Arangodb?
- javascript - 当用户输入一个新单词时如何使javascript自动完成重置?
- python - 我的陈述不起作用,我不知道为什么
- arrays - 在 Angular 上执行 GET 请求时出现此错误:找不到不同的支持对象 '[object Object]
- reactjs - 无法从 Firestore 集合中获取数据
- ruby-on-rails - Rails 的多个数据库不适用于远程数据库
- swift - SwiftUI x Firebase - 在执行代码之前无法等待 Firebase 结果
- reactjs - 无效的钩子调用 - React JS
- javascript - 使用 javascript 或 jquery 将 Html 非表单数据转换为 JSON