2

親レコードとすべての子レコードを複製する SQL ステートメントを (SQL Server で) 作成する最良の方法を見つけようとしています。例として以下があります。

-- duplicate the order

insert into 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
from Orders
where OrderID = @OrderID

-- find ID of duplicated order
declare @NewOrderID int;
select @NewOrderID = @@IDENTITY;

-- duplicate order details
insert into "Order Details" (OrderID, ProductID, UnitPrice, 
                            Quantity, Discount)
select @NewOrderID, ProductID, UnitPrice, Quantity, Discount
from "Order Details"
where OrderID = @OrderID

これは、子テーブル「Order Details」の複製に最適です。しかし、「Order Details」の子を複製する機能が必要ですが、それらの各レコードの ID を分離してさらに別のテーブルに渡す方法がわかりません。これを簡単に実現する方法について何か提案はありますか?

4

2 に答える 2

0

OUTPUT句の使用を検討しましたか。
興味があれば例を追加できます

于 2013-07-31T15:17:48.203 に答える