October 14, 2015 at 8:24 am
In order to improve readability and performance, can I simplify the query below to a set based query?
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!
October 14, 2015 at 8:50 am
Can I just use CROSS APPLY like below?
SELECT ProductCode
,OrderCode
,OrderDescription
FROM @ProductCodes PC
CROSS APPLY @OrderData OD
ORDER BY ProductCode
October 14, 2015 at 8:52 am
rs80 (10/14/2015)
In order to improve readability and performance, can I simplify the query below to a set based query?
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!
You mean like this?
INSERT INTO #FinalData(ProductCode, OrderID, OrderDescription)
SELECT p.ProductCode, o.OrderID, o.OrderDescription
FROM #ProductCodes p
CROSS JOIN #OrderData o
WHERE p.IsProcessed = 0
UPDATE #ProductCodes SET IsProcessed = 1 WHERE IsProcessed = 0
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 14, 2015 at 9:01 am
Thanks for your reply Chris.
Based on what I'm trying to achieve, cross JOIN seems to be the correct way instead of cross APPLY. I'm referencing this explaination: http://www.sqlservercentral.com/Forums/Topic843768-8-1.aspx
Please let me know if that's correct.
October 14, 2015 at 9:22 am
rs80 (10/14/2015)
Thanks for your reply Chris.Based on what I'm trying to achieve, cross JOIN seems to be the correct way instead of cross APPLY. I'm referencing this explaination: http://www.sqlservercentral.com/Forums/Topic843768-8-1.aspx
Please let me know if that's correct.
These statements
INSERT INTO #FinalData(ProductCode, OrderID, OrderDescription)
SELECT p.ProductCode, o.OrderID, o.OrderDescription
FROM #ProductCodes p
CROSS JOIN #OrderData o
WHERE p.IsProcessed = 0
INSERT INTO #FinalData(ProductCode, OrderID, OrderDescription)
SELECT p.ProductCode, o.OrderID, o.OrderDescription
FROM #ProductCodes p
CROSS APPLY #OrderData o
WHERE p.IsProcessed = 0
INSERT INTO #FinalData(ProductCode, OrderID, OrderDescription)
SELECT p.ProductCode, o.OrderID, o.OrderDescription
FROM #ProductCodes p
INNER JOIN #OrderData o ON 1 = 1
WHERE p.IsProcessed = 0
all have the same execution plan.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 14, 2015 at 9:38 am
But doesn't CROSS APPLY have a different purpose than CROSS JOIN?
October 14, 2015 at 9:50 am
rs80 (10/14/2015)
But doesn't CROSS APPLY have a different purpose than CROSS JOIN?
CROSS APPLY dbo.InlineTableValuedFunction (parameters)
I guess might be the standard usage of APPLY. So replace the tablesource dbo.InlineTableValuedFunction with an actual table, reduce the number of parameters to none, and there you have it. It's just a subset (or a special set) of the standard usage.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 14, 2015 at 9:50 am
rs80 (10/14/2015)
But doesn't CROSS APPLY have a different purpose than CROSS JOIN?
Yes, it allows you to reference columns from the previous tables to use in correlated subqueries or functions.
In this case, you logically need a CROSS JOIN which can be structured in any of the 3 options posted by Chris.
October 14, 2015 at 12:37 pm
Thanks Chris and Luis.
October 16, 2015 at 9:56 am
Does the original code that I posted with the while loop and the CROSS JOIN/APPLY have the same performance? I would think that the CROSS JOIN has better performance cause it's preserving the set based algorithm whereas the while loop is performing iteration.
October 16, 2015 at 10:07 am
rs80 (10/16/2015)
Does the original code that I posted with the while loop and the CROSS JOIN/APPLY have the same performance? I would think that the CROSS JOIN has better performance cause it's preserving the set based algorithm whereas the while loop is performing iteration.
Using the code I posted above and substituting your table names, compare the execution plans and look for any differences, ensuring that you look at the property sheets of the operators.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply