Hello,
I'm new to this forum, and wanted to ask for help. I'm struggling with the following issue,
I have a table Orders and a table OrderNumbers.
From the Orders Table I want to user the OrderNumber and the Quantity to insert multiple records into the OrderNumbers Table.
So if the Orders table contains:
ID Order Quantity
1 Order1 3
2 Order2 2
The OrderNumber table must look like this,
ID OrderNumber
1 Order1-1
1 Order1-2
1 Order1-3
2 Order2-1
2 Order2-2
Have tried to write a query, but It still doesn't work
SET @COUNTER = 1
SET @QTY = 1
WHILE @COUNTER IS NOT NULL AND @COUNTER <= @QTY
BEGIN
INSERT INTO OrderNumbers(
ID,
OrderNumber )
SELECT
ID,
Order + '-' + CONVERT(nvarchar(10),@Counter)
FROM Orders
SET @QTY = (SELECT QTY FROM OrderNumbers)
SET @COUNTER = @COUNTER + 1;
END;
But here the @Counter is not right, it inserts the wrong number of records.
Thanks in advance!