Question

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
Was it helpful?

Solution

On SQL Server 2008 you can use MERGE and the OUTPUT clause to get the mappings between the original and cloned id values from the insert into Orders then join onto that to clone the OrderItems.

DECLARE @IdMappings TABLE(
  New_OrderId INT,
  Old_OrderId INT)  

;WITH SourceOrders AS
(
SELECT *
FROM Orders
WHERE CustomerName = 'fred'
)
MERGE Orders AS T
USING SourceOrders AS S
ON 0 = 1
WHEN NOT MATCHED THEN
  INSERT (CustomerName )
  VALUES (CustomerName )
OUTPUT inserted.OrderId,
       S.OrderId INTO @IdMappings;  

INSERT INTO OrderItems
SELECT New_OrderId,
       LineNumber,
       Price,
       Notes
FROM   OrderItems OI
       JOIN @IdMappings IDM
         ON IDM.Old_OrderId = OI.OrderID
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top