Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 To insert a duplicate row?

Author  Topic 

jollyguy77
Starting Member

4 Posts

Posted - 2009-05-19 : 12:41:07
Hi,

I want to insert a duplicate row in a table. I want to insert a new row in a table that should have the same values as of the previous row and in addition to that, i will be changing 2 or 3 colums values from the previous row to the new row. For example.., i have the following columns in a table ID (primary key),orderid , jobid, added_by, comments, serialno, created_date, modified_date. I want to insert a new row that has the same values of orderid , jobid, added_by, comments, created_date, modified_date and in addition i will only change the serialno column, by getting the max(serialno) for the given orderid and jobid and increment it by 1 and then insert that into the new row. Can you tell me how it could be done?

Thanks,
Raja

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-19 : 13:16:55
[code]insert into yourtable (orderid , jobid, added_by, comments, serialno, created_date, modified_date)
select t.orderid , t.jobid, t.added_by, t.comments, t1.maxserialno+1, t.created_date, t.modified_date
from yourtable t
join (select orderid , jobid,max(serialno) as maxserialno
from yourtable
group by orderid , jobid) t1
on t1.orderid=t.orderid
and t1.jobid=t.jobid
and t1.maxserialno=t.serialno
[/code]
Go to Top of Page
   

- Advertisement -