简体   繁体   中英

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.

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). Most DDL statements do not allow variables in them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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