简体   繁体   English

创建表时如何使用变量作为标识列的种子

[英]How to use a variable as the seed to an identity column while creating tables

I want to put the set the seed of the identity column of a table based on some variable. 我想基于一些变量来设置表的标识列的种子。

For eg. 例如。 something like that. 这样的事情。

DECLARE @seed INT
SELECT @seed = MAX(id) 
  FROM tblSomeTable

Now, using @seed, I want to seed of my new table. 现在,使用@seed,我想为新表添加种子。

For eg. 例如。 something like this: 像这样的东西:

DECLARE @tempTable TABLE (
  ID INT IDENTITY(@seed,1) PRIMARY KEY, DESC NVARCHAR(50)
)

This actually throws an error saying incorrect syntax. 实际上,这会引发错误,说明语法不正确。 Is there any way to achieve this? 有什么办法可以做到这一点?

Use fully dynamic SQL and embed the seed value in the string containing the CREATE TABLE statement (or DECLARE ... TABLE statement). 使用完全动态的SQL,并将种子值嵌入包含CREATE TABLE语句(或DECLARE ... TABLE语句)的字符串中。 Most DDL statements do not allow variables in them. 大多数DDL语句都不允许使用变量。

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

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