If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Help with a sql procedure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-17-04, 15:23
cayenne cayenne is offline
Registered User
 
Join Date: Nov 2004
Posts: 3
Help with a sql procedure

Hi,

I have a table that has one id column that is unique, primary key, identifier... And counts 1 every time data is inserted into it!

Now I want to create a procedure that takes care of inserting data into the table and some other tables that are related to this table.
Of course, then I want to retreive the ID from the first column from the INSERT.. how do I do it ?
Reply With Quote
  #2 (permalink)  
Old 11-17-04, 16:07
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Use the parameter IDENTITY_VAL_LOCAL.
Reply With Quote
  #3 (permalink)  
Old 11-17-04, 16:08
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
What OS and DB2 version are you using?

If UDB V7.2 or greater, then look into Identity_Val_Local().

Andy
Reply With Quote
  #4 (permalink)  
Old 11-17-04, 16:21
cayenne cayenne is offline
Registered User
 
Join Date: Nov 2004
Posts: 3
Hi guys,

thanks for your quick response!

I use Windows XP and the latest version of DB2 (8,2?!) which I downloaded from the partner site at ibm.com a couple of days ago.

I will look into IDENTITY_VAL_LOCAL()

Can this variable retreive the value of the row that I just inserted?
Reply With Quote
  #5 (permalink)  
Old 11-17-04, 16:24
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
That is its sole purpose in life. Just be sure to use it right after the insert and before any commit.

Andy
Reply With Quote
  #6 (permalink)  
Old 11-17-04, 16:31
cayenne cayenne is offline
Registered User
 
Join Date: Nov 2004
Posts: 3
Quote:
Originally Posted by ARWinner
That is its sole purpose in life. Just be sure to use it right after the insert and before any commit.

Andy
Thanks man, you're the hero of the day!

Another question: Is the IDENTITY_VAL_LOCAL() always returning the id of the row that i inserted in my query? Or is it possible that I insert a row, and another person inserts a row a millisecond after and when I use the IDENTITY_VAL_LOCAL() I get the id that was created for the user that created the 2:nd row?
Reply With Quote
  #7 (permalink)  
Old 11-17-04, 16:44
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
It works only within the session it was invoked in. Only the value created in the session it is called in will be returned. Read the manual, it explains all this and more.

Andy
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On