sql server - SQL need to insert into table which only consists of a primary key but no auto increment -


i need insert value table consists of 1 column, is, primary key.

furthermore, null not allowed, identity set false , both identity seed , identity increment set 0.

i try insert insert table(id) values (null) not work. insert table(id) default values not work.

how can fill column correctly incremented id?

implementing identity or sequence best solution, if cannot alter schema alternative lock table in transaction, create new value, unlock table. note can have performance consequences.

create table dbo.ids ( id int primary key clustered ); go  insert dbo.ids values  ( 1 ), ( 2 ), ( 3 ), ( 4 ) ; go  declare @newid int;  begin transaction      set @newid = ( select top( 1 ) id dbo.ids ( tablockx, holdlock ) order id desc ) + 1 ;      insert dbo.ids values ( @newid );      select @newid;  commit  go 20 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -