473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to retrieve the value of a primary key

hi there,
pls how can i retrieve the value of a primary key in a table using a stored procedure.
i tried sp_pkeys but that just displays the column name of the primary key and not the value
thanks
Jun 14 '07 #1
3 2118
Purple
404 Expert 256MB
Hi olorunfunmi,

are you trying to get the value of the primary key for a row you have just inserted and the field has identity set on ?

if yes try:

Expand|Select|Wrap|Line Numbers
  1. Select @@identity as variable_name 
within the stored procedure

Regards Purple
Jun 14 '07 #2
Isn't SCOPE_IDENTITY( ) the better function to use? @@Identity just returns the last primary key which may have been in a different scope.

That is, if a another user called a stored procedure that ALSO created a new record my understanding is that the newest primary key would be returned - even it if was created for a different user in another scoped instance of that same stored procedure.
Jun 14 '07 #3
Purple
404 Expert 256MB
Hi DKelley,

It is an interesting question you raise and I guess the answer is yes and no :)

@@IDENTITY returns the last identity entered on the table by your current session (this is limited to your session only, you will not get identities entered by other users). While @@IDENTITY is limited to the current session, it's not limited to the current scope. therefore, if you have a trigger on a table that causes an identity to be created in another table, you will get the identity that was created last, even if it was the trigger that created it.

SCOPE_IDENTITY(), like @@IDENTITY, returns the last identity value created in the current session, but it additionally limits it to your current scope as well. In other words, it returns the last identity value you explicitly created, rather than any identity created by a trigger / user defined function.

There appears to be alot of misconception around @@IDENTITY - I guess as always the devil is in the detail..

Regards Purple
Jun 15 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Daniel Tonks | last post by:
First, please excuse the fact that I'm a complete MySQL newbie. My site used forum software that I wrote myself (in Perl) which, up until now, has used flat files. This worked fine, however...
0
by: refer_to_website | last post by:
I am using VB.NET to insert a record into an Access database. The database table uses an AutoNumber for its primary key. How can I determine what value Access assigned to the primary key? All...
2
by: Brad | last post by:
I'm extracting info for users and group in our domain using directoryservices. This is working well except I have one more piece I need to finish which I'm stuck on. I need to (a) query the info...
12
by: jaYPee | last post by:
I have currently using a dataset to access my data from sql server 2000. The dataset contains 3 tables that is related to each other. parent/child/grandchild relationship. My problem is it's very...
2
by: SueA | last post by:
Can anyone help me with the code to retrieve the name of the PrimaryKey field of the current table? For instance: qryMfrsAndBrands is based on the tables: tblBrands and tblMfrs...
13
by: kev | last post by:
Hi all, I have created a database for equipments. I have a form to register the equipment meaning filling in all the particulars (ID, serial, type, location etc). I have two buttons at the end...
12
bartonc
by: bartonc | last post by:
I just connected to the JET engine for the first time today using the Access ODBC connector and need some basics. I'm not even sure that Access (front end) experts are versed in SQL, but here goes:...
1
by: help4me | last post by:
I am doing a survey with many pages. Everyone is given the same password to get into the survey (bulk mailing) but since it is to be done anonymously, they have no unique id assigned to them. My...
12
lifeisgreat20009
by: lifeisgreat20009 | last post by:
I am a newbie to Struts and JSP...I have been working on the code below for 5 hours now..I googled a lot but couldn't get much help so finally I am here.. Hoping of getting my problem solved. Please...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.