In order to improve readability and performance, can I simplify the query below to a select query?
The jist of the query is to repeat the Order Information for each Product.
Thanks for the help!
Code:
DECLARE @ProductCodes TABLE(ProductCode nvarchar(3), IsProcessed bit DEFAULT 0)
INSERT INTO @ProductCodes(ProductCode) VALUES('ABC')
INSERT INTO @ProductCodes(ProductCode) VALUES('DEF')
INSERT INTO @ProductCodes(ProductCode) VALUES('GHI')
DECLARE @OrderData TABLE(OrderID nvarchar(3), OrderDescription nvarchar(1000))
INSERT INTO @OrderData VALUES('F12', 'Description1')
INSERT INTO @OrderData VALUES('F22', 'Description2')
DECLARE @FinalData TABLE(ProductCode nvarchar(3), OrderID nvarchar(3), OrderDescription nvarchar(1000))
DECLARE @ProductCode nvarchar(3)
WHILE ((SELECT COUNT(*) FROM @ProductCodes WHERE IsProcessed = 0) > 0)
BEGIN
SELECT TOP 1 @ProductCode = ProductCode
FROM @ProductCodes
WHERE IsProcessed = 0
INSERT @FinalData(ProductCode, OrderID, OrderDescription)
SELECT @ProductCode, OrderID, OrderDescription
FROM @OrderData
UPDATE @ProductCodes
SET IsProcessed = 1
WHERE ProductCode = @ProductCode
END
The jist of the query is to repeat the Order Information for each Product.
Thanks for the help!