sql server - T/SQL - Semi-manual Identity Column Insertion -


initial query

declare @table1 table (id int, value varchar(50)) declare @table2 table (value varchar(50)) declare @maxid1 int declare @maxid2 int = 52  insert @table1 (id, value) values (1,'one'),(2,'two'),(3,'three'),(4,'four'),(5,'five')  insert @table2 (value) values ('six'),('seven'),('eight'),('nine'),('ten')  select * @table1 select * @table2 select @maxid1 = max(id) @table1 select @maxid1 scenario1, @maxid2 scenario2 

expected outcome of @table1 after inserting values (value field) @table2

// (scenario #1 using @maxid1)

id  value 1   1 2   2 3   3 4   4 5   5 6   6 7   7 8   8 9   9 10  ten 

// (scenario #2 using @maxid2)

id  value 1   1 2   2 3   3 4   4 5   5 52  6 53  7 54  8 55  9 56  ten 

how semi-manually insert id values identity column (id) while executing insert query on table (@table1) table (@table2)? in first scenario, wish take max(id) @table1 , add 1 , keep adding records. in second scenario, wish take predefined # , start adding record first new record's id predefined values , subsequent new records max(id) + 1.

thank you

updated @table 1 not have identity constraint.

if want insert arbitrary int value identity column, use identity_insert:

set identity_insert [table_name] on; 

only 1 table @ time given session can have property on. make sure turn off when done:

set identity_insert [table_name] off; 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -