473,387 Members | 1,542 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,387 software developers and data experts.

Get Last Inserted IDENTITY

Hi, I need to get the last Identity from a table that was inserted into from a Stored Pocedure.

Im trying this and it doesn work.


(IN PartName VARCHAR(256),
IN PartPrice DECIMAL(19,2),
OUT PartID BIGINT)

INSERT INTO NULLID.PARTS
(PART_NAME, PART_PRICE)
VALUES
(PartName, PartPrice);

SET PartID = IDENTITY_VAL_LOCAL();

I read a post from Serge Rielau of IBM and tried this too but this gives me invalid conversion.

SELECT PART_ID INTO PartID FROM NEW TABLE(
INSERT INTO NULLID.PARTS
(PART_NAME, PART_PRICE)
VALUES
(PartName, PartPrice));


Im migrating from SQL Server to DB2 but my expirence has not been good. I can do this with a simple RETURN SCOPE_IDENTITY() in MSSQL. Can anyone tell me what im doing wrong? THANKS!!!
Apr 1 '07 #1
8 26052
This can be a little help to you.

http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/admin/r0004231.htm

Hi, I need to get the last Identity from a table that was inserted into from a Stored Pocedure.

Im trying this and it doesn work.


(IN PartName VARCHAR(256),
IN PartPrice DECIMAL(19,2),
OUT PartID BIGINT)

INSERT INTO NULLID.PARTS
(PART_NAME, PART_PRICE)
VALUES
(PartName, PartPrice);

SET PartID = IDENTITY_VAL_LOCAL();

I read a post from Serge Rielau of IBM and tried this too but this gives me invalid conversion.

SELECT PART_ID INTO PartID FROM NEW TABLE(
INSERT INTO NULLID.PARTS
(PART_NAME, PART_PRICE)
VALUES
(PartName, PartPrice));


Im migrating from SQL Server to DB2 but my expirence has not been good. I can do this with a simple RETURN SCOPE_IDENTITY() in MSSQL. Can anyone tell me what im doing wrong? THANKS!!!
Apr 2 '07 #2
chandu031
78 Expert
This can be a little help to you.

http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/admin/r0004231.htm
Hi,

The problem with your query "Select part_id into part_id..." is that it returns multiple values when you are inserting multiple rows. So all you have to do is use 'SELECT MAX(PART_ID) INTO PART_ID....

Hope this helps you....
Apr 2 '07 #3
Hi,

The problem with your query "Select part_id into part_id..." is that it returns multiple values when you are inserting multiple rows. So all you have to do is use 'SELECT MAX(PART_ID) INTO PART_ID....

Hope this helps you....
I don't think that will return multiple PART_ID's since that "New Table" only cares about that current scope. I wanted to use IDENTITY_VAL_LOCAL() since it's non deterministic but I havn't been able to get it to work. Inserts don't need a cursor but if I use the IDENTITY_VAL_LOCAL() function do I need to provide one? I want to put IDENTITY_VAL_LOCAL() in a output parameter. I also tried RETURN IDENTITY_VAL_LOCAL(); but that didn't work either. Thanks for the help so far guys I really appreciate it!!!!
Apr 2 '07 #4
chandu031
78 Expert
Hi,

If you are returning an output parameter from a procedure then a Return statement cannot be used. In order to use an output parameter from the called procedure in the calling procedure you will have to use

CALL <SCHEMA>.<CALLED_PROCNAME> (PART_ID);

where part id is an output parameter from the called procedure.. In the called procedure you just have to set the value

CREATE PROCEDURE <CALLED_PROCNAME>(OUT PART_ID BIGINT)
LANGUAGE SQL
BEGIN

INSERT......
SET PART_ID = IDENTITY_VAL_LOCAL();
END
Apr 3 '07 #5
Hi,

If you are returning an output parameter from a procedure then a Return statement cannot be used. In order to use an output parameter from the called procedure in the calling procedure you will have to use

CALL <SCHEMA>.<CALLED_PROCNAME> (PART_ID);

where part id is an output parameter from the called procedure.. In the called procedure you just have to set the value

CREATE PROCEDURE <CALLED_PROCNAME>(OUT PART_ID BIGINT)
LANGUAGE SQL
BEGIN

INSERT......
SET PART_ID = IDENTITY_VAL_LOCAL();
END
Hi chandu031,

I tried that and I get an Invalid Conversion error. I'm guessing I'm getting Null for the value and you can't convert Null to Integer. Is there anything else that I could be doing wrong?
Apr 3 '07 #6
chandu031
78 Expert
Hi chandu031,

I tried that and I get an Invalid Conversion error. I'm guessing I'm getting Null for the value and you can't convert Null to Integer. Is there anything else that I could be doing wrong?

Hi,

Casting NULL to integer wont raise an error as any operation done on NULL in DB2 results in a NULL. May be if I had some more information I would be able to figure out what the problem is.
Apr 4 '07 #7
Hi,

Casting NULL to integer wont raise an error as any operation done on NULL in DB2 results in a NULL. May be if I had some more information I would be able to figure out what the problem is.
All im tring to do is return the last identity inserted either by the return statement of an output parameter. Im using the .NET DB2 Framework. Im calling the DB from my VB.NET application. I set up a stored procedure that would insert a automotive part in the PARTS table and return me the Identity "PART_ID" of that insert. Can you post a stored proc that would do this? with RETURN statement or an output parameter? thanks!!!!
Apr 4 '07 #8
chandu031
78 Expert
All im tring to do is return the last identity inserted either by the return statement of an output parameter. Im using the .NET DB2 Framework. Im calling the DB from my VB.NET application. I set up a stored procedure that would insert a automotive part in the PARTS table and return me the Identity "PART_ID" of that insert. Can you post a stored proc that would do this? with RETURN statement or an output parameter? thanks!!!!

Hi,

Here's a sample proc:

CREATE PROCEDURE DB2ADMIN.GET_IDENTITY_AFTR_INSERT(IN L_PART_NAME VARCHAR(100), IN L_PART_PRICE INTEGER,OUT L_PART_ID BIGINT)
LANGUAGE SQL
BEGIN

INSERT INTO PARTS(PART_NAME,PART_PRICE) VALUES(L_PART_NAME,L_PART_PRICE);

VALUES IDENTITY_VAL_LOCAL() INTO L_PART_ID;

END


If this is not workin you can open a cursor on SELECT IDENTITY_VAL_LOCAL FROM SYSIBM.SYSDUMMY1.This is guaranteed to return one row. But since you are saying it is giving a "type conversion error" I don't think this is where you are facing a problem.
Apr 5 '07 #9

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

Similar topics

1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware ...
0
by: rodrigo | last post by:
How to get get the last inserted key (value NOT column) in a specific dataTable dt right after dt.Rows.InsertAt according to code and schema below. The key is a autoincrement column. foreach...
2
by: Danko Greiner | last post by:
I'm trying to update every table in DataSet, but getting inserted "id_user" gets me an exception: column "id_user" is read only if(_dataSet.HasChanges()) { SqlDataAdapter _adapter;...
3
by: Susanne Klemm | last post by:
Hello! I use a procedure to insert a new row into a table with an identity column. The procedure has an output parameter which gives me the inserted identity value. This worked well for a long...
2
by: imranabdulaziz | last post by:
Dear all, i am using asp.net ,C# (VS 2005) and sql server 2005. i have written sp for inserting the the data which written last inserted idendity no. i would like to which method should i...
13
rajiv07
by: rajiv07 | last post by:
Hi to all, I want to know how to select a last inserted record which is the primary key is not an integer. my table ramstr(Primary)----name--service XTC01-------Rajiv---service ...
20
by: canabatz | last post by:
hi all !! i got my results like that 1 user0 55 2 user1 53 3 user2 49 4 user2 48 5 user3 47 <------- last inserted row 6 user4 46
5
by: aniketk | last post by:
How can i get last inserted record. if i don't have any date column in table?
4
semanticnotion
by: semanticnotion | last post by:
Hi guys how can i retrieve the last inserted record from datebase. my primary key is not auto_increment its type is bigint because i want to insert SSN no as a primary key. when i retrive the data...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.