简体   繁体   English

在其他数据库中重新设定身份列(不在当前使用的数据库中)

[英]Reseed identity column in other database (not in database which is currently in use)

I would like to reseed identity column in a table, but not in the database which is currently in use.我想在表中重新设置身份列,但不在当前使用的数据库中。

This works:这有效:

use database;
DBCC CHECKIDENT ('table', reseed, 0);

But, I want to do something like this:但是,我想做这样的事情:

DBCC CHECKIDENT (database.[dbo].table, reseed, 0); -- works not
EXEC database.sys.sp_executesql N'DBCC CHECKIDENT(N''dbo.table'', reseed, 0);';

Or even:甚至:

DECLARE @t nvarchar(513) = N'dbo.table';

EXEC database.sys.sp_executesql 
   N'DBCC CHECKIDENT(@t, reseed, 0);', 
   N'@t nvarchar(513)', @t;

Also, if the table is empty and does not have any FK references or other limitations , you can do this easier using...此外,如果表是空的并且没有任何 FK 引用或其他限制,您可以使用...

TRUNCATE TABLE database.dbo.table;

...which happens to also reseed the IDENTITY column. ...这恰好也重新设定了IDENTITY列。

使用 EXEC 执行 USE 和 CHECKIDENT 怎么样?

EXEC('USE database;DBCC CHECKIDENT (''dbo.table'', reseed, 0);')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM