(
ProductID int,
Revenue money
)
INSERT INTO @ProductTotals (ProductID, Revenue)
SELECT ProductID, SUM(UnitPrice * Quantity)
FROM [Order Details]
GROUP BY ProductID
select * from @ProductTotals
While connected to the Northwind data-base, we could write the following SELECT statement to populate the table variable.
You can use table variables in batches, stored procedures, and user-defined functions (UDFs). We can UPDATE records in our table variable as well as DELETE records
UPDATE
@ProductTotals
SET Revenue = Revenue * 1.15
WHERE ProductID = 62
DELETE
FROM @ProductTotals
WHERE ProductID = 60
SELECT TOP 5 *
FROM @ProductTotals
ORDER BY Revenue DESC
You might think table
variables work just like temporary tables (CREATE TABLE #ProductTotals),
but there are some differences.
No comments:
Post a Comment