View previous topic :: View next topic |
Author |
Message |
tattva Beginner
Joined: 02 Feb 2005 Posts: 97 Topics: 36
|
Posted: Sat Feb 11, 2006 2:00 am Post subject: Insert the Missing Seq num in a table or Max value + 1 |
|
|
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 |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Sat Feb 11, 2006 8:32 am Post subject: |
|
|
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 |
|
 |
|
|