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 > Identity or Sequence in DB2 Stored Procedure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-15-11, 02:29
ashu000 ashu000 is offline
Registered User
 
Join Date: Apr 2011
Posts: 28
Identity or Sequence in DB2 Stored Procedure

Hi All,

One of my table which is already existing in which a column named "Sequence" is there which has not been declared as Identity and logic was getting handled in the application code.

Now, I need to create a Stored Procedure in DB2 which will do the same operation because we want to reduce the time consumption.

I tried different ways but not able tp find a way to declare this column as identity in my procedure.



I am looking for suggestions that if either it is possible?

If possible kindly help me.

Thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 04-15-11, 08:35
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
It is not clear what you are trying to do in the stored procedure ? Can you post what you have already tried ?

What version of db2 are you on ?
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 04-15-11, 08:36
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Generally speaking, you would use the ALTER TABLE statement to do what. What alterations you can and cannot do, depend on the DB2 version, which you chose not to disclose.
Reply With Quote
  #4 (permalink)  
Old 04-20-11, 02:34
ashu000 ashu000 is offline
Registered User
 
Join Date: Apr 2011
Posts: 28
Quote:
Originally Posted by sathyaram_s View Post
It is not clear what you are trying to do in the stored procedure ? Can you post what you have already tried ?

What version of db2 are you on ?
Hi,

Sorry for the late reply.

I am able to achieve, what I was looking for after following these steps on dummy tables where I am declaring my column as Identity.
But, in my real scenario, clolumn in which I need to put values in a sequence is not declared as Identity column.

Here I am explaining what I have done for dummy tables:

CREATE PROCEDURE PRAP (IN YEAR CHAR(2), IN NUMBER CHAR (5))
LANGUAGE SQL
BEGIN

FOR i AS state CURSOR FOR SELECT DISTINCT STATE FROM FILE2
DO
INSERT INTO FILE1 VALUES (YEAR,NUMBER,DEFAULT,state);
END FOR;
END@

I create table FILE1 as :
CREATE TABLE FILE1 (YEAR CHAR(2), NUMBER CHAR(5),SEQ_NBR BIGINT NOT NULL GENERATED BY DEFAULT (START WITH 1 MINVALUE 1 NO CACHE)), STATE CHAR(3);

This is working, but when I am trying something like this on my existing table, it is not working for me.

Kindly suggest.
Reply With Quote
  #5 (permalink)  
Old 04-20-11, 13:45
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
CREATE TABLE inside a stored procedure has to be done via dynamic SQL, i.e. you have to create the statement as string and then use EXECUTE IMMEDIATE to execute it dynamically.

Remember that a procedure is usually called multiple times. So creating tables inside procedures usually doesn't make much sense. Only the first call would succeed and all subsequent ones fail because the table already exists.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Tags
db2, stored procedure

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