July 31, 2014 at 3:01 pm
I am trying to add multiple records to my table (insert/select).
INSERT INTO Users
( User_id ,
Name
)
SELECT ( SELECT MAX(User_id) + 1
FROM Users
) ,
Name
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 duplicate
This would insert about 20 records.
Why the error?
Thanks,
Tom
July 31, 2014 at 3:06 pm
tshad (7/31/2014)
I am trying to add multiple records to my table (insert/select).
INSERT INTO Users
( User_id ,
Name
)
SELECT ( SELECT MAX(User_id) + 1
FROM Users
) ,
Name
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 duplicate
This would insert about 20 records.
Why the error?
Thanks,
Tom
Try something like (untested but certain it works ๐
๐
INSERT INTO Users
( User_id ,
Name
)
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) + ( SELECT MAX(User_id)
FROM Users
) ,
Name
July 31, 2014 at 3:55 pm
To further explain things (hopefully correctly), the reason your SQL was failing was that it was determining the MAX value once, and adding 1 to it once, and attempting to use that same value for each of the 20 new records you were inserting. Make sense?
I'd guess that if you used your method to insert 1 record, it would work fine.
August 1, 2014 at 10:41 am
That worked fine.
Not sure what this means:
ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
How does that work?
August 1, 2014 at 11:34 am
tshad (8/1/2014)
That worked fine.Not sure what this means:
ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
How does that work?
This adds an incremental number to the output, by stating (SELECT NULL), one indicates that the order is not implied by any values but only the order of appearance. So for each row returned, the id value will be the MAX(KEY) (static throughout the batch) + the incremental row number.
๐
August 19, 2014 at 12:33 pm
CELKO (8/19/2014)
First learn that a row is not a record; this is a fundamental concept.Next, look at CREATE SEQUENCE.
Hey Joe,
would you care to share a definition of the difference of the two?
๐
August 19, 2014 at 12:37 pm
CELKO (8/19/2014)
First learn that a row is not a record; this is a fundamental concept.Next, look at CREATE SEQUENCE.
depends on what you are referring to;
taken at face value Wikipedia disagrees with that statement in the very first sentence of the definition
http://en.wikipedia.org/wiki/Row_(database)
http://en.wikipedia.org/wiki/Row_(database)In the context of a relational database, a rowโalso called a record or tuple โrepresents a single, implicitly structured data item in a table. In simple terms, a database table can be thought of as consisting of rows and columns or fields.[1] Each row in a table represents a set of related data, and every row in the table has the same structure.
Lowell
August 19, 2014 at 10:19 pm
CELKO (8/19/2014)
would you care to share a definition of the difference of the two?
You'll be sorry; this is the short version ๐
Thank you for this and not at all sorry as this is what was missing from your previous post ๐ In my opinion, complete answers such as this one help the OP's building a better understanding of the subject. It also aids in battling certain growing ambiguity.
๐
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply