SQL Server 重置所有表的标识增量
基本上我需要将所有表的身份增量重置为其原始值。 这里我尝试了一些代码,但失败了。
链接中的实际代码:
USE World00_Character
GO
-- Create a cursor to loop through the System Ojects and get each table name
DECLARE TBL_CURSOR CURSOR
-- Declare the SQL Statement to cursor through
FOR ( SELECT Name FROM Sysobjects WHERE Type='U' )
-- Declare the @SQL Variable which will hold our dynamic sql
DECLARE @SQL NVARCHAR(MAX);
SET @SQL = '';
-- Declare the @TblName Variable which will hold the name of the current table
DECLARE @TblName NVARCHAR(MAX);
-- Open the Cursor
OPEN TBL_CURSOR
-- Setup the Fetch While that will loop through our cursor and set @TblName
FETCH NEXT FROM TBL_CURSOR INTO @TblName
-- Do this while we are not at the end of the record set
WHILE (@@FETCH_STATUS <> -1)
BEGIN
-- Appeand this table's select count statement to our sql variable
SET @SQL = @SQL + ' ( SELECT '''+@TblName+''' AS Table_Name,COUNT(*) AS Count FROM '+@TblName+' ) UNION';
-- Delete info
EXEC('DBCC CHECKIDENT ('+@TblName+',RESEED,(SELECT IDENT_SEED('+@TblName+')))');
-- Pull the next record
FETCH NEXT FROM TBL_CURSOR INTO @TblName
-- End the Cursor Loop
END
-- Close and Clean Up the Cursor
CLOSE TBL_CURSOR
DEALLOCATE TBL_CURSOR
-- Since we were adding the UNION at the end of each part, the last query will have
-- an extra UNION. Lets trim it off.
SET @SQL = LEFT(@SQL,LEN(@SQL)-6);
-- Lets do an Order By. You can pick between Count and Table Name by picking which
-- line to execute below.
SET @SQL = @SQL + ' ORDER BY Count';
--SET @SQL = @SQL + ' ORDER BY Table_Name';
-- Now that our Dynamic SQL statement is ready, lets execute it.
EXEC (@SQL);
GO
错误消息:
Error: Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '('.
我如何修复该 SQL 或将所有表的身份重置为其原始身份?
Basically I need to reset Identity Increment for all tables to its original.
Here I tried some code, but it fails.
Actual code from link:
USE World00_Character
GO
-- Create a cursor to loop through the System Ojects and get each table name
DECLARE TBL_CURSOR CURSOR
-- Declare the SQL Statement to cursor through
FOR ( SELECT Name FROM Sysobjects WHERE Type='U' )
-- Declare the @SQL Variable which will hold our dynamic sql
DECLARE @SQL NVARCHAR(MAX);
SET @SQL = '';
-- Declare the @TblName Variable which will hold the name of the current table
DECLARE @TblName NVARCHAR(MAX);
-- Open the Cursor
OPEN TBL_CURSOR
-- Setup the Fetch While that will loop through our cursor and set @TblName
FETCH NEXT FROM TBL_CURSOR INTO @TblName
-- Do this while we are not at the end of the record set
WHILE (@@FETCH_STATUS <> -1)
BEGIN
-- Appeand this table's select count statement to our sql variable
SET @SQL = @SQL + ' ( SELECT '''+@TblName+''' AS Table_Name,COUNT(*) AS Count FROM '+@TblName+' ) UNION';
-- Delete info
EXEC('DBCC CHECKIDENT ('+@TblName+',RESEED,(SELECT IDENT_SEED('+@TblName+')))');
-- Pull the next record
FETCH NEXT FROM TBL_CURSOR INTO @TblName
-- End the Cursor Loop
END
-- Close and Clean Up the Cursor
CLOSE TBL_CURSOR
DEALLOCATE TBL_CURSOR
-- Since we were adding the UNION at the end of each part, the last query will have
-- an extra UNION. Lets trim it off.
SET @SQL = LEFT(@SQL,LEN(@SQL)-6);
-- Lets do an Order By. You can pick between Count and Table Name by picking which
-- line to execute below.
SET @SQL = @SQL + ' ORDER BY Count';
--SET @SQL = @SQL + ' ORDER BY Table_Name';
-- Now that our Dynamic SQL statement is ready, lets execute it.
EXEC (@SQL);
GO
Error message:
Error: Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '('.
How can I either fix that SQL or reset identity for all tables to its original?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您是否有很多没有种子且增量为 1 的表?
如果没有(默认情况下,所有表都有),请使用以下代码:
MSforeachtable
是一个未记录但非常方便的存储过程,它对数据库中的所有表执行给定的命令。如果您需要绝对精确,请使用此语句 - 它将生成一个 SQL 语句列表,将所有表重新设定为其原始 SEED 值:
获取输出中的最后一列,然后执行这些语句,您就完成了! :-)
(受到 博客文章,作者:Pinal Dave)
Do you have lots of tables which do not have a seed and increment of 1 ??
If not (by default, all tables have that), use this code:
MSforeachtable
is an undocumented, but extremely handy stored proc which executes a given command against all tables in your database.If you need to be absolutely exact, use this statement - it will generate a list of SQL statements to reseed all tables to their original SEED value:
Grab that last column in the output, and execute those statements and you're done! :-)
(inspired by a blog post by Pinal Dave)
对 marc_s 的答案稍作调整。
那些围绕 ? 的单引号性格很重要。该语句将导致 SQL Server 自动重新计算每个表的下一个标识值。
Slight tweak on marc_s answer.
Those single-quotes around the ? character are important. That statement will cause SQL Server to automatically recalculate the next identity value for each table.
轻微的变化可以更好地处理模式......
Slight variation that handles Schemas a bit better...
使用此命令时要小心,如果您的表包含数据,则所有新插入都会导致重复错误,
以解决您需要运行此命令的问题,
如果数据存在,这会将种子重置为最后一个列标识
Be careful when using this command if your table contains data all your new inserts will result duplicate error
to solve the problem you need to run this after that
this will reset the seed to the last column identity if the data exists
要仅对具有标识列的表重新设定种子,您可以使用下一个脚本。
它还使用
sp_MSforeachtable
但考虑到正确的表。To reseed ONLY tables with an identity column you can use the next script.
It also makes use of
sp_MSforeachtable
but taking into account the correct tables.使用 sp_MSForEachTable 并在重置之前检查表是否具有标识值的另一种方法:
注意:如果您希望标识值从 1 开始,则使用 DBCC 命令应使用
CHECKIDENT (''?'', RESEED, 0)
而不是CHECKIDENT (''?'', RESEED, 1)
,如某些答案中所示。引自 MS SQL Server 文档:Another way of using
sp_MSForEachTable
and checking whether or not the Table has an identity value before resetting it:NOTE: If you want the identity value to start at 1 then the DBCC command should use
CHECKIDENT (''?'', RESEED, 0)
notCHECKIDENT (''?'', RESEED, 1)
as indicated in some of the answers. Quote from MS SQL Server documentation:一个简单的方法可能是使用 sp_MSforeachtable 命令,这是一个未记录但相对众所周知的命令,用于查看表。
An easy metod may be to use the sp_MSforeachtable command, an undocumented, but relatively well know command that looks over your tables.
我的工作场所按计划停电,不知出于什么原因,我的一些表的身份列中的数字跃升了 1000。
我修改了此代码以创建一个选择字符串列表,以显示与当前标识相比的列的当前值。
然后,我将这些行复制并粘贴到另一个选择中。
然后复制其中选定的列,
然后您可以从复制 DBC_String 列
Table 修改您需要的表格
We had a scheduled power outage at my work and for what ever reason some of my tables had jumped by 1000 in their Identity Column.
I modified this code to make a list of select strings to show the current Value of the column compared to the Current Identity.
I then copy and pasted the rows into another selection.
Then copy the selected column inside of
then you can copy the DBC_String column from the
Table to modify the tables you need
使用下面的代码,
Use the below code,
(我从另一个SO页面重新发布我的答案)
也许最简单的方法(听起来很疯狂,看起来代码很臭)就是像这样运行
DBCC CHECKIDENT
两次:完成。
如果需要,您可以再次运行它以查看所有种子的设置:
这只是利用文档中的注释的一种创造性方法:
(I'm reposting my answer from this other SO page)
Perhaps the easiest way (as crazy as this sounds and as code-smelly as it looks) is to just run
DBCC CHECKIDENT
twice like this:Done.
If you want, you can run it once more to see what all the seeds were set to:
This is just a creative way to take advantage of the comment from the documentation: