I have 2 tables:
- Order (with a identity order id field)
- OrderItems (with a foreign key to order id)
In a stored proc, I have a list of orders that I need to duplicate. Is there a good way to do this in a stored proc without a cursor?
Edit:
This is on SQL Server 2008.
A sample spec for the table might be:
CREATE TABLE Order (
OrderID INT IDENTITY(1,1),
CustomerName VARCHAR(100),
CONSTRAINT PK_Order PRIMARY KEY (OrderID)
)
CREATE TABLE OrderItem (
OrderID INT,
LineNumber INT,
Price money,
Notes VARCHAR(100),
CONSTRAINT PK_OrderItem PRIMARY KEY (OrderID, LineNumber),
CONSTRAINT FK_OrderItem_Order FOREIGN KEY (OrderID) REFERENCES Order(OrderID)
)
The stored proc is passed a customerName of ‘fred’, so its trying to clone all orders where CustomerName = ‘fred’.
To give a more concrete example:
Fred happens to have 2 orders:
- Order 1 has line numbers 1,2,3
- Order 2 has line numbers 1,2,4,6.
If the next identity in the table was 123, then I would want to create:
- Order 123 with lines 1,2,3
- Order 124 with lines 1,2,4,6
On SQL Server 2008 you can use
MERGEand theOUTPUTclause to get the mappings between the original and clonedidvalues from the insert intoOrdersthen join onto that to clone the OrderItems.