I have a bunch of data which will insert into a table. This issue is that I need it to return the primary key to that table. I wasn’t sure if there was things like:
insert into TABLE (...) values (...) RETURNING p_key
or
select p_key from (insert into TABLE (...) values (...))
I am making a workaround for a browser and saved information which will more or less add a row and then update it… but without the primary key, there is no way to update it as there is no reference to it.
I was looking online and found some examples via google, but it confused me slightly with these examples.
http://en.wikipedia.org/wiki/Insert_(SQL)#Retrieving_the_key
Wikipedia was saying that for SQL Server 2008 to use OUTPUT instead of RETURNING, possible to use something like OUTPUT p_key
If you’re inserting a whole set of rows, selecting the
SCOPE_IDENTITY()won’t do. AndSCOPE_IDENTITYalso only works for (numeric) identity columns – sometimes your PK is something else…But SQL Server does have the
OUTPUTclause – and it’s very well documented on MSDN!Those values will be “echoed” back to the calling app, e.g. you’ll see them in a grid in SQL Server Management Studio, or you can read them as a result set from your C# or VB.NET calling this
INSERTstatement.