【发布时间】:2017-11-08 20:24:04
【问题描述】:
我正在尝试使用OUTPUT 获取新插入行的 ID。但是,我遇到了 HY010 错误。以下查询/代码是我使用的:
string = """
SET NOCOUNT ON;
DECLARE @NEWID TABLE(ID INT);
INSERT INTO dbo.t1 (Username, Age)
OUTPUT inserted.id INTO @NEWID(ID)
VALUES(?, ?)
SELECT ID FROM @NEWID
"""
cursor.execute(string, "John Doe", 35)
cursor.commit()
id = cursor.fetchone()[0]
最后一行 id = cursor.fetchone()[0] 导致 HY010 错误(见下文)。任何建议将不胜感激!
pyodbc.Error: ('HY010', '[HY010] [Microsoft][ODBC SQL Server Driver]Function sequence error (0) (SQLFetch)')
【问题讨论】:
-
您是否尝试过只执行插入然后访问
cursor.lastrowid? python.org/dev/peps/pep-0249/#lastrowid -
我收到一条错误消息
AttributeError: pyodbc.Cursor object has no attribute lastrowid.
标签: python sql-server pyodbc