-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTABLE_variable.sql
More file actions
42 lines (38 loc) · 763 Bytes
/
TABLE_variable.sql
File metadata and controls
42 lines (38 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
Table Variables: TABLE data type can be used to store a result set
- Often called a Table Variable
Advantages:
- Very Fast
- Automatically cleaned-up
Disadvantages:
- Can significantly impact performance if used to store more than 100 rows of data
- Not supported in the query optimizer's cost-based reasoning
*/
DECLARE @mtl TABLE
(
FirstName varchar(40)
,LastName varchar(40)
,Orders int
,OrderTot int
)
INSERT INTO @mtl
(
FirstName
,LastName
,Orders
,OrderTot
)
SELECT
p.FirstName
,p.LastName
,COUNT(*) NumberOrders
,SUM(soh.TotalDue) Totals
FROM
Person.Person p
JOIN
Sales.SalesOrderHeader soh
ON p.BusinessEntityID = soh.SalesPersonID
GROUP BY FirstName, LastName
SELECT *
FROM @mtl
ORDER BY Orders DESC