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 > incrementing numbers for insert statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-29-11, 01:05
techday techday is offline
Registered User
 
Join Date: Nov 2011
Posts: 24
incrementing numbers for insert statement

DB2 Z/OS VERSION 10.1

create table mytable (
sno int not null,
desc varchar(15)
)
sno has unique index.

I have a insert statement that gets its values from a select statement

insert into mytable
(desc)
select column2 from table2;

Is there a way I can populate the sno value with sequential numbers without using identity/sequence number/rownumber?
Essentially something like i+1?

I tried the following, (table2 has 10 rows)
but all the rows selected have the same value (2) and index is violated...
declate i int;
set i = 1;
insert into mytable
(sno,desc)
select i+1,column2 from table2;

If its possible what would the correct syntax be?
Reply With Quote
  #2 (permalink)  
Old 11-29-11, 05:57
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
techday, I am not in a place to test this out but this should work:
Code:
INSERT INTO MYTABLE
WITH MAX_TAB(SNO_MAX)
  AS (
      SELECT MAX(SNO)
      FROM MYTABLE
     )
SELECT ROW_NUMBER() OVER(ORDER BY COLUMN2) + SNO_MAX, COLUMN2
FROM MAX_TAB
   , TABLE2
However, if more than one person is running the process at one time, there could be situations where SNO value is duplicated.
Reply With Quote
  #3 (permalink)  
Old 11-29-11, 14:46
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
This should work for DB2 LUW (but not sure about DB2 z/OS) :

Code:
CREATE TABLE "DBA     "."VENDOR"  (
                  "VENDOR_ID" INTEGER NOT NULL ,
                  "VENDOR_NM" VARCHAR(25) )
                 IN "USERSPACE1" ;

ALTER TABLE "DBA     "."VENDOR"
        ADD CONSTRAINT "PK_VENDOR" PRIMARY KEY
                ("VENDOR_ID");

CREATE TABLE "DBA     "."NEXT_AVAIL_ID"  (
                  "APPLICATION_ID" SMALLINT NOT NULL ,
                  "NEXT_ID" INTEGER NOT NULL )
                 IN "USERSPACE1" ;

ALTER TABLE "DBA     "."NEXT_AVAIL_ID"
        ADD CONSTRAINT "PK_NEXT_AVAIL_ID" PRIMARY KEY
                ("APPLICATION_ID");


create procedure dba.test ()
begin
declare v_next_id int;
set v_next_id = (select next_id from final table (update dba.next_avail_id set next_id = next_id + 1 where application_id = 1));
insert into dba.vendor values (v_next_id, 'IBM');
commit;
end@
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #4 (permalink)  
Old 12-05-11, 04:01
techday techday is offline
Registered User
 
Join Date: Nov 2011
Posts: 24
Quote:
Originally Posted by Stealth_DBA View Post
techday, I am not in a place to test this out but this should work:
Code:
INSERT INTO MYTABLE
WITH MAX_TAB(SNO_MAX)
  AS (
      SELECT MAX(SNO)
      FROM MYTABLE
     )
SELECT ROW_NUMBER() OVER(ORDER BY COLUMN2) + SNO_MAX, COLUMN2
FROM MAX_TAB
   , TABLE2
However, if more than one person is running the process at one time, there could be situations where SNO value is duplicated.
Stealth, that was a nice trick to add to row number.
But I do not want to use row number...
Also, small modifcation to do IFNULL in case mytable has no records..
Code:
INSERT INTO MYTABLE
WITH MAX_TAB(SNO_MAX)
  AS (
      SELECT IFNULL(MAX(SNO),0)
      FROM MYTABLE
     )
SELECT ROW_NUMBER() OVER(ORDER BY COLUMN2) + SNO_MAX, COLUMN2
FROM MAX_TAB
   , TABLE2
Reply With Quote
  #5 (permalink)  
Old 12-05-11, 04:04
techday techday is offline
Registered User
 
Join Date: Nov 2011
Posts: 24
Quote:
Originally Posted by Marcus_A View Post
This should work for DB2 LUW (but not sure about DB2 z/OS) :

Code:
create procedure dba.test ()
begin
declare v_next_id int;
set v_next_id = (select next_id from final table (update dba.next_avail_id set next_id = next_id + 1 where application_id = 1));
insert into dba.vendor values (v_next_id, 'IBM');
commit;
Marcus, not sure how this works..
First we need to have a row inserted in dba.next_avail_id with application_id 1.?
Even after this, I think only one row is going to have the next seq number.
But i need to assign seq num for mutiple rows with source as another select.
Reply With Quote
  #6 (permalink)  
Old 12-05-11, 04:08
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
Quote:
Originally Posted by techday View Post
Marcus, not sure how this works..
First we need to have a row inserted in dba.next_avail_id with application_id 1.?
Even after this, I think only one row is going to have the next seq number.
But i need to assign seq num for mutiple rows with source as another select.
My example allows for multiple next available numbers for multiple tables. But inserts for a given table would only use one of the next available number rows designated by the application_id.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #7 (permalink)  
Old 12-05-11, 04:24
techday techday is offline
Registered User
 
Join Date: Nov 2011
Posts: 24
ok thanks.
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