在SQL查询中多次使用相同的参数
我正在使用PYODBC和Microsoft SQL Server,
我正在尝试在Python中复制一个存储过程,在该过程中,对于
SELECT *
FROM
(
SELECT
SurveyId,
QuestionId,
1 as InSurvey
FROM
SurveyStructure
WHERE
SurveyId = @currentSurveyId
UNION
SELECT
@currentSurveyId as SurveyId,
Q.QuestionId,
0 as InSurvey
FROM
Question as Q
WHERE NOT EXISTS
(
SELECT *
FROM SurveyStructure as S
WHERE S.SurveyId = @currentSurveyId AND S.QuestionId = Q.QuestionId
)
) as t
ORDER BY QuestionId
Python中的每个@CurrentsUrveyId执行此查询,我到目前为止都可以使用:
cursor.execute("""SELECT UserId FROM dbo.[User]""")
allSurveyID = cursor.fetchall()
for i in allSurveyID:
p = i
test = cursor.execute("""SELECT *
FROM
(
SELECT
SurveyId,
QuestionId,
1 as InSurvey
FROM
SurveyStructure
WHERE
SurveyId = (?)
UNION
SELECT
(?) as SurveyId,
Q.QuestionId,
0 as InSurvey
FROM
Question as Q
WHERE NOT EXISTS
(
SELECT *
FROM SurveyStructure as S
WHERE S.SurveyId = (?)AND S.QuestionId = Q.QuestionId
)
) as t
ORDER BY QuestionId""",p)
for i in test:
print(i)
该参数可在使用时使用(如果我从Union of Union wardswards中删除所有内容) 。在查询的其余部分中尝试使用相同的参数时,我会收到以下错误:('SQL包含3个参数标记,但提供了1个参数','hy000')
是否可以多次使用相同的参数在同一查询中?
谢谢
I am using pyodbc and Microsoft SQL Server
I am trying to replicate a stored procedure in python where this query is executed for every @currentSurveyId
SELECT *
FROM
(
SELECT
SurveyId,
QuestionId,
1 as InSurvey
FROM
SurveyStructure
WHERE
SurveyId = @currentSurveyId
UNION
SELECT
@currentSurveyId as SurveyId,
Q.QuestionId,
0 as InSurvey
FROM
Question as Q
WHERE NOT EXISTS
(
SELECT *
FROM SurveyStructure as S
WHERE S.SurveyId = @currentSurveyId AND S.QuestionId = Q.QuestionId
)
) as t
ORDER BY QuestionId
In Python, I so far have:
cursor.execute("""SELECT UserId FROM dbo.[User]""")
allSurveyID = cursor.fetchall()
for i in allSurveyID:
p = i
test = cursor.execute("""SELECT *
FROM
(
SELECT
SurveyId,
QuestionId,
1 as InSurvey
FROM
SurveyStructure
WHERE
SurveyId = (?)
UNION
SELECT
(?) as SurveyId,
Q.QuestionId,
0 as InSurvey
FROM
Question as Q
WHERE NOT EXISTS
(
SELECT *
FROM SurveyStructure as S
WHERE S.SurveyId = (?)AND S.QuestionId = Q.QuestionId
)
) as t
ORDER BY QuestionId""",p)
for i in test:
print(i)
The parameter works when used once (if I delete everything from UNION onwards). When trying to use the same parameter in the rest of the query, I get the following error:('The SQL contains 3 parameter markers, but 1 parameters were supplied', 'HY000')
Is it possible to use the same parameter multiple times in the same query?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PYODBC本身仅支持“ Qmark”(位置)参数(ref:),但是对于T-SQL(Microsoft SQL Server),我们可以使用匿名代码块避免多次传递相同的参数值:
pyodbc itself only supports "qmark" (positional) parameters (ref: here), but with T-SQL (Microsoft SQL Server) we can use an anonymous code block to avoid having to pass the same parameter value multiple times:
如果重复使用相同的参数值,只需乘以参数的一个单项列表:
还请考虑
左JOIN
(或完整加入)需要两个Qmarks:可能是可能的 :即使是一个参数:
If reusing the same parameter value, simply multiply a one-item list of the parameter:
Consider also refactoring your SQL for
LEFT JOIN
(orFULL JOIN
) requiring two qmarks:Possibly even for one parameter: