sql server - SQL for duplicating parent and children records -
i'm trying figure out best way create sql statement (in sql server) duplicate parent record , children records. have below example;
-- duplicate order insert orders (customerid, employeeid, orderdate, requireddate, shippeddate, shipvia, freight, shipname, shipaddress, shipcity, shipregion, shippostalcode, shipcountry) select customerid, employeeid, getdate(), requireddate, null, shipvia, freight, shipname, shipaddress, shipcity, shipregion, shippostalcode, shipcountry orders orderid = @orderid -- find id of duplicated order declare @neworderid int; select @neworderid = @@identity; -- duplicate order details insert "order details" (orderid, productid, unitprice, quantity, discount) select @neworderid, productid, unitprice, quantity, discount "order details" orderid = @orderid
this works duplicating child table "order details". need ability duplicate children of "order details" see no way of isolating identity of each of records , passing yet table. have suggestions how can accomplished?
to find out latest identity insert please try :
select @neworderid=ident_current('tablename')
Comments
Post a Comment