[英]How do I insert from a table variable to a table with an identity column, while updating the the identity on the table variable?
[英]How to use a variable as the seed to an identity column while creating tables
我想基于一些变量来设置表的标识列的种子。
例如。 这样的事情。
DECLARE @seed INT
SELECT @seed = MAX(id)
FROM tblSomeTable
现在,使用@seed,我想为新表添加种子。
例如。 像这样的东西:
DECLARE @tempTable TABLE (
ID INT IDENTITY(@seed,1) PRIMARY KEY, DESC NVARCHAR(50)
)
实际上,这会引发错误,说明语法不正确。 有什么办法可以做到这一点?
使用完全动态的SQL,并将种子值嵌入包含CREATE TABLE
语句(或DECLARE ... TABLE
语句)的字符串中。 大多数DDL语句都不允许使用变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.