[英]@@Identity returns 0 from Access Frontend (SQL Server Express backend)
I have been all over the web looking for a way to fix this, and all the answers seem to tell me that I am doing this correctly. 我一直在网上寻找解决此问题的方法,所有答案似乎都告诉我我做得正确。 I have inherited this Access / SQL Server database, and I cannot seem to get this piece to work. 我已经继承了该Access / SQL Server数据库,但似乎无法使这一部分正常工作。
I am executing a fairly basic SQL INSERT
query that works fine. 我正在执行工作正常的相当基本的SQL INSERT
查询。 I have checked it several times. 我已经检查了好几次了。 Once the query executes, I am attempting to get the identity value back out of it to use for the rest of my code. 查询执行后,我试图从中获取身份值以用于其余代码。
Here is my code: 这是我的代码:
db.Execute (SQLStr)
Set rs = db.OpenRecordset("SELECT @@IDENTITY")
InNo = rs(0)
Debug.Print InNo
rs.Close
db.Close
This always returns a value of 0 for InNo
. 这始终为InNo
返回值0。 I have tried several different methods but I cannot seem to get this working. 我尝试了几种不同的方法,但似乎无法正常工作。
I have also used this method from the SQL Server directly, and querying the @@Identity
works fine. 我也直接在SQL Server中使用了此方法,并且查询@@Identity
效果很好。
Thanks in advance. 提前致谢。
I'm suspecting that you are running SELECT @@IDENTITY
on your local Access database, which of course doesn't know what @@IDENTITY
is. 我怀疑您在本地Access数据库上运行SELECT @@IDENTITY
,这当然不知道@@IDENTITY
是什么。
I'm not 100% sure, though, because you didn't show your whole code and I don't know what db
is. 不过,我不确定100%,因为您没有显示完整的代码,也不知道db
是什么。
If db
is CurrentDB
, then the Set rs = db.OpenRecordset
line opens a DAO.Recordset
which does execute the query on your local Access database. 如果db
是CurrentDB
,则Set rs = db.OpenRecordset
行将打开一个DAO.Recordset
,它会在本地Access数据库上执行查询。
If my suspicion is correct, then you need to open the recordset where you execute SELECT @@IDENTITY
via pass-through query , so it actually runs on the SQL Server. 如果我的猜想是正确的,那么您需要打开记录集,并通过传递查询在其中执行SELECT @@IDENTITY
的记录集实际上在SQL Server上运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.