简体   繁体   English

@@ Identity从Access Frontend(SQL Server Express后端)返回0

[英]@@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. 如果dbCurrentDB ,则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.

相关问题 将Access 2007 accdb后端切换到SQL Server Express /最佳前端选项? - Switching Access 2007 accdb backend to SQL server express / Best frontend options? 使用SPROC从MS Access前端在SQL Server后端中插入记录 - Using SPROCs to insert records in SQL Server backend from MS Access frontend 建立审计系统; SQL Server后端上的MS Access前端 - Building an auditing system; MS Access frontend on SQL Server backend 带有 SQL 服务器后端的 MS Access 前端 - integer 密钥超出范围 - MS Access frontend with SQL Server backend - integer key is out of range SQL Server后端,MS Access前端:连接 - SQL-Server Backend, MS Access Frontend: Connection 使用SQL Server后端访问前端,可在某些计算机上运行,​​而不是在其他计算机上运行 - Access frontend with SQL Server backend, works on some machines, not others Access 2010 前端将查询传递给 SQL Server 2014 后端 - Access 2010 Frontend Passing Queries to an SQL Server 2014 Backend Access 97前端-SQL Server 2005后端链接表错误 - Access 97 Frontend - SQL Server 2005 Backend Linked Table Error SQL Server后端和Access前端 - 与SQL Server本机客户端的ODBC连接失败 - SQL Server backend and Access frontend - ODBC Connection to SQL Server native client failed 将后端从MS Access迁移到SQL Server - Migrating the backend from MS Access to SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM