访问& SQL-Server:通过表擦除重置自动编号字段
我试图在 SQL Server Express 表和生产 Access 版本之间保持活动表的副本同步。在其他表中,我能够擦除 SQL Server 表的内容,然后将整个乱七八糟的内容插回以捕获所有更改。但是,当我尝试对此表执行此操作时 - 我得到了信息 - 但自动编号字段从上一次迭代中最后一个未使用的数字开始递增。经过十几次“同步”操作后,我已经危险地接近用完该字段的自动编号了。
我尝试从 Microsoft Access 前端应用程序发出“DBCC CHECKIDENT”,这会引发错误,指出该语句不是以 SELECT、DELETE、PROCEDURE、DROP、ALTER 或类似内容开头。当从 SQL Server 管理控制台(我在当前测试环境中确实可以访问该工具,但在应用程序投入生产时可以访问该工具)发出此命令时,该命令确实有效。
然后,我使用 DoCmd.RunSQL 命令尝试“TRUNCATE TABLE”查询,它抛出了链接表不支持该操作的错误(大约)。该表链接到 Microsoft Access 前端(后端表位于 SQL Server Express 上)。
因此,快速总结一下:
- 前端是一个 Microsoft Access VBA 应用程序
- 数据存储在远程计算机上的 SQL Server Express 上
- 数据表在前端应用程序中链接
- 我将无法使用此应用程序时的 SQL Server 管理控制台命令投入生产后,它需要自己进行内务处理。
- DoCmd.RunSQL 和 CurrentDB.Execute 似乎不允许使用 TRUNCATE TABLE -或 - 'DBCC CHECKIDENT' 命令。
- 当从 SQL Server 管理控制台发出时,上述两个函数都可以工作 - 请参阅上文了解为什么这不是一个可行的选项。
- -除此之外,所有其他表的行为方式均符合我的预期,并根据需要重置其标识字段。
-- 编辑于 08/08/2011 @ 15:08 --
好吧 - 我已经尝试了多次尝试基于 VBA 的直通查询,所有这些都导致 ODBC -- 调用失败错误。以下是我创建的用于处理传递的模块的代码(从 dbforums.com 借用了代码):
Function RunPassThrough(ByVal ConnectionString As String, ByVal SQL As String, Optional ByVal QueryName As String)
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Set dbs = CurrentDb
Set qdf = dbs.CreateQueryDef
With qdf
.Name = QueryName
.Connect = ConnectionString
.SQL = SQL
.ReturnsRecords = (Len(QueryName) > 0)
If .ReturnsRecords = False Then
.Execute
Else
If Not IsNull(dbs.QueryDefs(QueryName).Name) Then dbs.QueryDefs.Delete QueryName
dbs.QueryDefs.Append qdf
End If
.Close
End With
Set qdf = Nothing
Set dbs = Nothing
End Function
因此,我需要指定一个到数据库的连接字符串;以下所有操作均失败:
strConnect = "ODBC;DRIVER={SQL Server};SERVER=ENV980-067\ENVIRON_TEST;DATABASE=instkeeper_test;Uid=<my username>;Pwd=<my password>;"
结果:运行时错误 ODBC -- 调用失败(错误 #3146)
strConnect = "ODBC;DRIVER={SQL Server};SERVER=ENV980-067\ENVIRON_TEST;DATABASE=instkeeper_test;TRUSTED_CONNECTION=YES;"
结果:运行时错误 ODBC -- 调用失败(错误 #3146)
strConnect = "ODBC;DSN=instkeeper_beta;"
结果:询问我一次数据源名称使用数据源管理面板指定时,我收到运行时错误 ODBC -- 调用失败(错误 #3146)
strConnect = "ODBC;Server=ENV980-067\ENVIRON_TEST;Database=instkeeper_test;User ID=<my user name>;Password=<my password>;Trusted_Connection=False;"
结果:运行时错误 ODBC -- 调用失败(错误 #3146)
-- 已编辑08/08/2011 @ 16:41 --
连接字符串迭代出现更多失败,我正式对如何让这个野兽工作失去了想法。在尝试了之前的方法之后,现在在请求 DSN 后,使用该接口进行的任何传递都会失败。无法修复,必须恢复它们才能调用链接表并通过 JET 运行。
strConnect = "ODBC;DATA SOURCE=instkeeper_test;"
结果:运行时错误 ODBC -- 调用失败(错误 #3146)
strConnect = "ODBC;DRIVER=SQL Server;SERVER=ENV980-067\ENVIRON_TEST;"
结果:运行时错误 ODBC -- 调用失败(错误 #3146)
strConnect = "ODBC;DRIVER=SQL Server;Server=ENV980-067\ENVIRON_TEST;Database=instkeeper_test;User ID=<my user name>;Password=<my password>;"
结果:运行时错误 ODBC -- 调用失败(错误 #3146)
I am trying to keep a copy of an activity table synchronized between a SQL Server Express table and the production Access version. In other tables, I am able to wipe the contents of the SQL Server table, and insert the whole mess back in to capture all of the changes. However, when I attempt to do that with this table - I get the information - but the Autonumber field increments from the last unused number from the previous iteration. After a dozen or so 'sync' operations, I am dangerously close to running out of autonumbers for this field.
I have tried issuing the 'DBCC CHECKIDENT' from the Microsoft Access front end application, which throws an error that the statement didn't start with SELECT, DELETE, PROCEDURE, DROP, ALTER or something like that. This command DOES work when issued from the SQL Server management console (a tool I do have access to in the current test environment, but will NOT when the application goes production).
I then tried the 'TRUNCATE TABLE' query using the DoCmd.RunSQL command and it threw the error that the operation isn't supported on linked tables (approximately). This table is linked to the Microsoft Access front-end (the back end table is on SQL Server Express).
So, for a quick summary:
- Front End is an Microsoft Access VBA application
- Data is stored on SQL Server Express on a remote machine
- Data tables are linked in the front end application
- I will NOT be able to use SQL Server Management Console commands when this application goes production, it needs to run it's housekeeping on it's own.
- DoCmd.RunSQL and CurrentDB.Execute do not seem to allow the use of the TRUNCATE TABLE -or- the 'DBCC CHECKIDENT' command.
- Both of the aforementioned functions -DO- work when issued from the SQL Server Management Console - see above as to why this isn't a viable option.
- -ALL- other tables behave the way I'd expect them to besides this one, resetting their indentity fields as appropriate.
-- Edited 08/08/2011 @ 15:08 --
Alright - I have tried a number of attempts at a VBA-based pass-through query, all resulting in an ODBC -- call failed error. Here is the code for the module I created to handle pass-throughs (borrowed the code from dbforums.com):
Function RunPassThrough(ByVal ConnectionString As String, ByVal SQL As String, Optional ByVal QueryName As String)
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Set dbs = CurrentDb
Set qdf = dbs.CreateQueryDef
With qdf
.Name = QueryName
.Connect = ConnectionString
.SQL = SQL
.ReturnsRecords = (Len(QueryName) > 0)
If .ReturnsRecords = False Then
.Execute
Else
If Not IsNull(dbs.QueryDefs(QueryName).Name) Then dbs.QueryDefs.Delete QueryName
dbs.QueryDefs.Append qdf
End If
.Close
End With
Set qdf = Nothing
Set dbs = Nothing
End Function
As such, I need to specify a connection string to the database; all of the following have failed:
strConnect = "ODBC;DRIVER={SQL Server};SERVER=ENV980-067\ENVIRON_TEST;DATABASE=instkeeper_test;Uid=<my username>;Pwd=<my password>;"
Result: Run-time error ODBC -- call failed (error #3146)
strConnect = "ODBC;DRIVER={SQL Server};SERVER=ENV980-067\ENVIRON_TEST;DATABASE=instkeeper_test;TRUSTED_CONNECTION=YES;"
Result: Run-time error ODBC -- call failed (error #3146)
strConnect = "ODBC;DSN=instkeeper_beta;"
Result: Asks me for the Data Source Name, once specified with the Data Source administration panel, I get Run-time error ODBC -- call failed (error #3146)
strConnect = "ODBC;Server=ENV980-067\ENVIRON_TEST;Database=instkeeper_test;User ID=<my user name>;Password=<my password>;Trusted_Connection=False;"
Result: Run-time error ODBC -- call failed (error #3146)
-- Edited 08/08/2011 @ 16:41 --
MORE failures on iterations of the connection strings, I am officially out of ideas on how to make this beast work. After trying the previous - now any pass-throughs made with the interface fail after asking for a DSN. No repair is possible, they have to be restored to call on the linked tables and ran through JET.
strConnect = "ODBC;DATA SOURCE=instkeeper_test;"
Result: Run-time error ODBC -- call failed (error #3146)
strConnect = "ODBC;DRIVER=SQL Server;SERVER=ENV980-067\ENVIRON_TEST;"
Result: Run-time error ODBC -- call failed (error #3146)
strConnect = "ODBC;DRIVER=SQL Server;Server=ENV980-067\ENVIRON_TEST;Database=instkeeper_test;User ID=<my user name>;Password=<my password>;"
Result: Run-time error ODBC -- call failed (error #3146)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 SQL Server 端,您可以
使用另一个 DBCC 命令
DBCC CHECKIDENT ('MyTable', RESEED, 1)
或者使用截断表...
所以你会跑
In the SQL Server side, you can
Use another DBCC command
DBCC CHECKIDENT ('MyTable', RESEED, 1)
Or use TRUNCATE TABLE...
So you'd run
我检查了所有代码,并咨询了当地的 VBA 专家,他指出我在尝试执行直通查询时在表命名中犯了错误。基本上,我是通过前端中链接所拥有的名称来引用该表,而不是后端 SQL Server 中它所拥有的实际表名称。
更正后,以下连接字符串就起作用了:
然后,这允许我在远程表上执行 TRUNCATE TABLE 命令,并执行 DoCmd.RunSQL 语句以从生产源重新填充该表。
最终的结果是,当选择更新Activity的选项时,它将清除远程表的内容,然后将生产表的内容读入测试表中以供使用,同时重置自动编号。
I went through all of my code, and checked with a local VBA expert who pointed out that I had made an error in the naming of my tables while attempting to do a pass-through query. I, basically, was referring to the table by the name it possess as a link in my front-end, and not the actual table name it possesses in SQL Server on the back end.
The following connection string, once this was corrected, worked:
This, then, allowed me to execute the TRUNCATE TABLE command on the remote table, and execute a DoCmd.RunSQL statement to repopulate the table from the production source.
The end result is that when the option is selected to update the Activity, it will purge the contents of the remote table, then read the contents of the production table into the test table for use while resetting the autonumber.
我发现了一种简单的方法,通过在 Access 中的 SQL 视图中编写 SQL 查询来实现这一点!
检查一下:
http://answers.microsoft.com/en-us/office/forum/office_2003-access/reset-autonumber-in-access-table-automatically/66cbcfed-5cbe-40f6-b939-9aea8bbea2de< /a>
写下:
ALTER TABLE YourTable ALTER COLUMN YourField计数器(1,1)
I found out an easy way of doing that trought a SQL query written in Access, at the SQL View!
Check it out at:
http://answers.microsoft.com/en-us/office/forum/office_2003-access/reset-autonumber-in-access-table-automatically/66cbcfed-5cbe-40f6-b939-9aea8bbea2de
Write this:
ALTER TABLE YourTable ALTER COLUMN YourField COUNTER(1,1)