Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Adding unique int key using max

$
0
0
I am trying to add multiple records to my table (insert/select).[code] INSERT INTO Users ( User_id , Name ) SELECT ( SELECT MAX(User_id) + 1 FROM Users ) , Name[/code]But I get the error:Violation of PRIMARY KEY constraint 'PK_Users'. Cannot insert duplicate key in object 'dbo.Users'.But I am using the max User_id + 1, so it can't be duplicateThis would insert about 20 records.Why the error?Thanks,Tom

Viewing all articles
Browse latest Browse all 3145

Trending Articles