我正在处理sql server数据库,
我有一个名为' table1 ' 的表,包含1列和1行
exp_num
0
Run Code Online (Sandbox Code Playgroud)
我正在尝试将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
Run Code Online (Sandbox Code Playgroud)
当我在运行这个时,SQL editor我得到了结果.
Oldexp_num Newexp_num
0 1
Run Code Online (Sandbox Code Playgroud)
但如果我把它作为一个查询,并尝试使用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()
Run Code Online (Sandbox Code Playgroud)
当我做cursor.fetchone()时,我得到以下错误.
File "<ipython-input-1398-bdaba305080c>", line 1, in <module>
cursor.fetchone()
ProgrammingError: No results. Previous SQL was not a query.
Run Code Online (Sandbox Code Playgroud)
pyodbc包中有错误吗?或者在我的查询中
| 归档时间: |
|
| 查看次数: |
1498 次 |
| 最近记录: |