gpt4 book ai didi

python - pyodbc.ProgrammingError : No results. 一次执行多个语句时,以前的 SQL 不是查询

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:21 25 4
gpt4 key购买 nike

我正在处理 sql server 数据库,

我有一个名为“table1”的表,其中包含 1 列和 1 行

exp_num
0

我正在尝试将 0 值 exp_num 列更新为 +1,同时返回旧实验和更新后的实验。

为此我使用了 declare 语句。

DECLARE @UpdateOutput1 table (Oldexp_num int,Newexp_num int);

UPDATE get_exp_num
SET exp_num = exp_num+1

OUTPUT
DELETED.exp_num,
INSERTED.exp_num
INTO @UpdateOutput1;

select * from @UpdateOutput1

当我在 SQL 编辑器 中运行它时,我得到了结果。

Oldexp_num   Newexp_num
0 1

但是如果我将其与查询相同,并尝试使用 pyodbc 包,我会收到错误消息。

import pyodbc

connection = pyodbc.connect() # i am getting a connection
query = "DECLARE @UpdateOutput1 table (Oldexp_num int,Newexp_num int);UPDATE get_exp_num SET exp_num = exp_num+1 OUTPUT DELETED.exp_num, INSERTED.exp_num INTO @UpdateOutput1; select Newexp_num from @UpdateOutput1;"
cursor = connection.cursor()
cursor.execute(query)
cursor.fetchone()

当我执行 cursor.fetchone() 时,出现以下错误。

File "<ipython-input-1398-bdaba305080c>", line 1, in <module>
cursor.fetchone()

ProgrammingError: No results. Previous SQL was not a query.

pyodbc包有没有错误?或者在我的查询中

最佳答案

通过在匿名代码块的开头添加 SET NOCOUNT ON; 解决了这个问题。该语句抑制由 DML 语句(如 UPDATE ...)生成的记录计数值,并允许直接检索结果集。

关于python - pyodbc.ProgrammingError : No results. 一次执行多个语句时,以前的 SQL 不是查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48061304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com