更新时间:2023-02-05 23:12:36
如果您使用表变量,则无法执行.如果它是一个表,则可以截断它或使用DBCC CHECKIDENT
.但是,如果必须使用表变量,则必须使用Identity列以外的其他东西.或者,更准确地说,使用表变量中的identity列,但使用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 using ROWNUMBER
:
DECLARE @t table (pkint int IDENTITY(1,1), somevalue nvarchar(50))
INSERT INTO @t (somevalue) VALUES( 'one')
INSERT INTO @t (somevalue) VALUES('twp')
INSERT INTO @t (somevalue) VALUES('three')
SELECT row_number() OVER (ORDER BY pkint), somevalue FROM @t
DELETE FROM @t
INSERT INTO @t (somevalue) VALUES('four')
SELECT row_number() OVER (ORDER BY pkint), somevalue FROM @t
***使用table变量.
It's the best you can do with the table variable.