如何在 T-SQL 表变量中重新植入标识列?
我有一个 T-SQL 表变量(不是表),它有一个自动递增的标识列。 我想清除该变量中的所有数据并将标识列值重置为 1。如何做到这一点?
I have a T-SQL table variable (not a table) which has an auto incrementing identity column. I want to clear all data from this variable and reset the identity column value to 1. How can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果需要在 while 循环的每一轮中截断表变量,可以将
declare @myTbl (...)
语句放入循环中。 这将重新创建表并在循环的每一轮重置标识列。 然而,它对性能造成了严重影响。 我有相当紧的循环,并且重新声明相对于删除@myTbl的表变量要慢几倍。If you need to truncate the table variable in each turn of a while loop, you can put the
declare @myTbl (...)
statement in the loop. This will recreate the table and reset the identity column on each turn of the loop. However, it has a heavy performance hit. I had fairly tight loop, and redeclaring the table variable relative todelete @myTbl
was several times slower.当我想在使用 SQL 2000 时使用 TOP 和变量时,我就这样做了。基本上,您添加记录,然后查看最小记录。 我遇到了同样的问题并注意到了这个线程。 删除表不会重置种子,尽管我想象使用 GO 应该删除表和变量来重置种子。
上面查询中的 @maxlimit 是为了获取查询的前 900 个,并且由于表变量将具有不同的起始标识键,这将解决该问题。
任何后续查询都可以减去该派生过程以使其插入为“1”等。
I did this when I wanted to use a TOP and a variable when using SQL 2000. Basically, you add in the records and then look at the minimum one. I had the same problem and noticed this thread. Deleting the table doesn't reset the seed although I imagine using GO should drop the table and variable to reset the seed.
@maxlimit in the query above was to get the top 900 of the query and since the table variable would have a different starting identity key, this would solve that issue.
Any subsequent query can subtract that derived procedure to make it insert as "1", etc.
我建议您使用两个表变量。 @Table1 在第一列上有一个身份种子。 @Table2 具有相同的第一列,但其上没有身份种子。
当您循环处理流程时,
请在流程循环时从两个表中删除。
在第一次通过时,@Table2 将在第一行中具有从 1 开始的序列号。
第二次循环时,您的第二个表可能在第一列中具有从 1081 开始的序列号。但是如果您选择最小值到一个变量
然后你可以更新@Table2以使RowID从1开始,如下所示:
希望这有帮助
I suggest you use two table variables. The @Table1 has an identity seed on the first column. @Table2 has the same first column but no identity seed on it.
As you loop through your process,
then Delete From both Tables as your Process Loops.
On your first pass, the @Table2 will have a a sequential number in the first row starting at 1.
The second time through the loop your second table might have sequential numbers in the first column starting at say 1081. But if you select the minimum value to a variable
Then you can update @Table2 to make RowID start at 1 as follows:
Hope this helps
截断表将转储所有数据,并重置身份种子。
否则,您可以使用此调用来重置身份,同时保留任何数据:
Truncating the table will dump ALL the data, and reset the identity seed.
Otherwise, you can use this call to reset the identity while retaining any of the data:
如果您使用的是表变量,则无法执行此操作。 如果它是一个表,您可以截断它或使用
DBCC CHECKIDENT
。 但是,如果您必须使用表变量,则必须使用标识列以外的其他内容。 或者,更准确地说,使用表变量中的标识列,但使用ROWNUMBER
进行输出:这是您可以对表变量执行的最佳操作。
If you're using a table variable, you can't do it. If it were a table, you could truncate it or use
DBCC CHECKIDENT
. But, if you have to use a table variable, you have to use something other than an identity column. Or, more accurately, use the identity column in your table variable but output usingROWNUMBER
:It's the best you can do with the table variable.