MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Insert the Missing Seq num in a table or Max value + 1

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
tattva
Beginner


Joined: 02 Feb 2005
Posts: 97
Topics: 36

PostPosted: Sat Feb 11, 2006 2:00 am    Post subject: Insert the Missing Seq num in a table or Max value + 1 Reply with quote

Hi All,

There is a db2 program which basically inserts some rows into the DB2 table.
If there any duplicate rows , then the program creates a new Primary key ( finds the latest key and adds +1 to that ).

Primary key in this case is Product Num.

Instead of adding +1 to the key , i need to find out all the missing Prod num and use them.

Ex:-

Assume Prod num in the DB2 table are :-

1
2
3
4
6
7
8

Now my program creates a new prod num ( i.e. 9) and inserts the otehr fields.

Instead of 9 , i would like to use 5.

Can this be done using SQL query alone?

PLease let me know if there are any other ways of achieving this

Thanks
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Sat Feb 11, 2006 8:32 am    Post subject: Reply with quote

tattva,

Try this sql to find the missing prod_num and use that value to insert into the table.

Code:

SELECT MIN(A.PROD_NUM) + 1 AS MISSING_NO           
  FROM TABLE A                                     
 WHERE NOT EXISTS ( SELECT PROD_NUM                 
                      FROM TABLE B                 
                     WHERE B.PROD_NUM = A.PROD_NUM + 1)
                   ;                           


Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group