Solved

returning primary key after update

Posted on 2013-01-04
2
388 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
2 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
Comment Utility
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
Comment Utility
I understand  Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sql server lock cursor 13 39
Updating ms sql with special characters 8 19
Sql query for filter 12 21
BULK LOGGED - log full 9 11
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now