forked from SeemaSP/shoppingjquerycart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4 - TempTableVTableVar.sql
66 lines (47 loc) · 1.2 KB
/
4 - TempTableVTableVar.sql
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- Demonstration 3B
-- Step 1: Open a new query window to the tempdb database
USE people;
GO
-- Step 2: Create a temporary table
CREATE TABLE #People
(
personid UNIQUEIDENTIFIER,
firstname VARCHAR(80),
lastname VARCHAR(80),
dob DATETIME,
dod DATETIME,
sex CHAR(1)
);
GO
-- Step 3: Populate the table
INSERT #People
SELECT TOP(250)*
FROM dbo.people
-- Step 4: Query the table and show actual row estimates
SELECT count(*) FROM #People;
GO
-- Step 5: Disconnect and reconnect
-- (right-click the query window and choose Connection then Disconnect,
-- then right-click the query window and choose Connection then Connect,
-- in the Connect to Server window, click Connect)
-- Step 6: Attempt to query the table again (this will fail)
USE tempdb;
GO
SELECT count(*) FROM #People;
GO
DROP TABLE #People
-- Do the same with a Table Variable
DECLARE @people TABLE
(
personid UNIQUEIDENTIFIER,
firstname VARCHAR(80),
lastname VARCHAR(80),
dob DATETIME,
dod DATETIME,
sex CHAR(1)
)
INSERT @people
SELECT TOP(250)*
FROM dbo.people
-- Now run the select. It will fail. Run it all as a batch
SELECT count(*) FROM @people