是否有一种方法可以使用pyodbc向sql server 2005插入一个请求scope_identity()

时间:2022-04-25 22:24:22

I have this great pyodbc lib. I try the code below, it supposed to insert a row and return the row id but it didn't work. by the way I'm using sql server 2005 on server and client is windows os

我有这个很棒的pyodbc库。我尝试下面的代码,它应该插入一行并返回行id,但是它不起作用。顺便说一下,我在服务器上使用sql server 2005,客户端是windows os

...
con = pyodbc.connect('conectionString', autocommit = True)
cur = con.execute(
                  "insert into sometable values('something');
                  select scope_identity() as id"
                  )
for id in cur:
   print id
...

some idea?

一些主意吗?

1 个解决方案

#1


5  

Try this, one statement with the OUTPUT clause

试试这个,带有OUTPUT子句的语句

cur = con.execute(
         "insert into sometable OUTPUT INSERTED.idcolumn values('something')"
         )

Edit: This should get around the issue commented by Joe S.

编辑:这应该可以绕过乔·S评论的问题。

#1


5  

Try this, one statement with the OUTPUT clause

试试这个,带有OUTPUT子句的语句

cur = con.execute(
         "insert into sometable OUTPUT INSERTED.idcolumn values('something')"
         )

Edit: This should get around the issue commented by Joe S.

编辑:这应该可以绕过乔·S评论的问题。