Click here to Skip to main content
15,904,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a table XYZ which contains a column "srno". It has no constraint(s), no Sequences, no trigger, I have assumed that the srno is unique. The table has no relation with any other table.

My client has a requirement that when a row is deleted, The deleted "srno" should be reused.

How can I accomplish this using SQL Query.
Till now I am using
SQL
select max(nvl(Srno,0))+1 from xyz
to Find out the new SRNO which will return the missing or deleted srno for Re-Use!

Prathamesh
Posted

I would insert the deleted SRNO into another table and if that table has rows use an SRNO from that table (delete it when done). If it has no rows you can use the method you outlined above.
 
Share this answer
 
there are two ways, you can add new records to missing srno which is deleted, like if you have 1 to 10 records having srno in sequence 1,2,3 etc. and you are deleting the 3rd srno, and now you are inserting new record then first of all you need to find out the missing srno and use that srno while you are inserting new entry.

or

if you want something like this, 1,2,3...10 and you are deleting 3rd srno then rest of the entries are auto adjust with proper sequence like 1,2,3,4,5,6,7,8,9, so for this you need to fire update query every time to update proper srno to rest of the records whenever you are deleting any record.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900