Solved

returning primary key after update

Posted on 2013-01-04
2
400 Views
Last Modified: 2013-01-04
I use this appended to an INSERT statement (using Dim PK as long = Command.ExecuteScalar() ) to return the primary key:

SELECT @@Identity

How do I do the same for an UPDATE?  I am geting a NULL back.
0
Comment
Question by:HLRosenberger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
ID: 38745503
First off, read this about @@IDENTITY:

http://msdn.microsoft.com/en-us/library/ms187342.aspx

That value is populated as the value of the last identity value generated by a statement.

An update statement shouldn't be automatically generating new identity values (unless you're somehow playing with the value of the identity column?)

In an update statement you're dealing with an existing row, not a newly generated one.

Select the value of the identity column you're updating from the table after the update - the value will be there, it's not something that's newly generated.

If I have a new table with an identity column and I insert 5 rows, @@IDENTITY should return 5 (assuming I start with 1 and increment by 1).

If I come back later and try to update row #5, I need to know the value of the row I'm updating to somehow identify it.  I can select from the table after the update where there is some means of identifying the row (a customer number or PO number or whatnot) and check the value of the identity column as a part of that query.
0
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 38745518
I understand  Thanks!
0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question