且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

@@ IDENTITY之后的INSERT语句总是返回0

更新时间:2023-01-12 17:37:49

1)结合INSERT和SELECT语句(使用;连接)到1 db命令中

1) combine the INSERT and SELECT statement (concatenate using ";") into 1 db command

2)使用SCOPE_IDENTITY ()而不是@@ IDENTITY

2) use SCOPE_IDENTITY() instead of @@IDENTITY

INSERT INTO blabla ...; SELECT OID FROM table WHERE OID = SCOPE_IDENTITY()

INSERT INTO blabla... ; SELECT OID FROM table WHERE OID = SCOPE_IDENTITY()

- 更新:

问题与MS ACCESS有关,我发现这篇文章,这表明只需重复使用第一个命令并将其CommandText设置为SELECT @@ IDENTITY应该就足够了。

as it turned out that the question was related to MS ACCESS, I found this article which suggests that simply reusing the first command and setting its CommandText to "SELECT @@IDENTITY" should be sufficient.