August 30, 2011 at 7:48 am
So table variables can only be used in cursor functions.
What's the best way to handle something like this?
declare @tbtemp table(temp1 int, temp2 int);
insert into @tbtemp (temp1,temp2)
SELECT SUM(ProductSales.UnitsSold) AS sumUS, CategoryProductRel.CategoryID
FROM CurrentCategoryList INNER JOIN
CategoryProductRel ON CurrentCategoryList.CategoryID = CategoryProductRel.CategoryID INNER JOIN
ProductSales ON CategoryProductRel.ProductID = ProductSales.ProductID INNER JOIN
ProductSalesPopularity ON ProductSales.ProductSalesPopularityID = ProductSalesPopularity.ProductSalesPopularityID
WHERE (ProductSalesPopularity.ProductSalesPopularityID = 203)
GROUP BY CategoryProductRel.CategoryID;
update CurrentCategoryList set CurrentCategoryList.TotalUnitSoldNational = @tbtemp.temp1
FROM @tbtemp INNER JOIN
CurrentCategoryList ON @tbtemp.temp2 = CurrentCategoryList.CategoryID;
August 30, 2011 at 7:59 am
foxjazzG (8/30/2011)
So table variables can only be used in cursor functions.
Not as far as I know. What are you trying to do?
John
August 30, 2011 at 9:16 am
Correct me if I am wrong but wouldn't this code make your data pretty useless if you run it more than once? You are updating a column in a table with the sum of it's records. What about running it a second time? Like John said... "What are you trying to do?"
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
August 30, 2011 at 9:18 am
Table variables can be used just about anywhere that permanent or temp tables can
The only syntax problem I can see with your queries is that table variables have to be aliased to refer to them elsewhere in the query, like this:
UPDATE CurrentCategoryList
SET CurrentCategoryList.TotalUnitSoldNational = t.temp1
FROM @tbtemp t
INNER JOIN CurrentCategoryList ON t.temp2 = CurrentCategoryList.CategoryID;
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
August 30, 2011 at 9:24 am
For that matter you could probably just skip the temp table like this:
update CurrentCategoryList set CurrentCategoryList.TotalUnitSoldNational = SUM(ProductSales.UnitsSold)
FROM CurrentCategoryList INNER JOIN
CategoryProductRel ON CurrentCategoryList.CategoryID = CategoryProductRel.CategoryID INNER JOIN
ProductSales ON CategoryProductRel.ProductID = ProductSales.ProductID INNER JOIN
ProductSalesPopularity ON ProductSales.ProductSalesPopularityID = ProductSalesPopularity.ProductSalesPopularityID
WHERE (ProductSalesPopularity.ProductSalesPopularityID = 203)
GROUP BY CategoryProductRel.CategoryID;
--edit spelling.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply