我正在处理 sql server 数据库,
我有一张名为 'table1' 包含 1 列和 1 行
exp_num
0
我正在尝试更新0
将 exp_num 列值设置为 +1,并返回旧实验和更新后的实验。
为此,我使用声明语句。
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 editor
我正在得到结果。
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包有错误吗?或者在我的查询中