Previous | Table of Contents | Next |
When Would I Use a Local Temp Table?
Pretend for a moment that the pubs database is actually much larger.
Note: If youre using the SQL Web interface, you wont have to pretendyou can use the bigpubs database.
You have some work to do related to the authors and titles tables. But today youre only responsible for authors who live in California. You know from experience that, because of the table sizes and the overhead necessary to perform a table join, it could take a few minutes to resolve some of your more complex queries. You also know that you will be working with the California author data and their titles, all day long. Instead of asking the server to sort through all the author data, you can make its life (and, consequently, yours) faster and easier by making a copy of only California authors.
I will create a local temp table containing only a few columns from authors and titles:
select a.au_id, a.au_lname, a.au_fname, a.phone, a.contract, ta.au_ord, ta.royaltyper, ta.title_id, t.title, t.type, t.price, t.advance, t.royalty into #MyTA from authors a, titles t, titleauthor ta where a.au_id = ta.au_id and ta.title_id = t.title_id and a.state = CA
(17 row(s) affected)
Now, if you selected everything from your new table, it would look like the following. (The table has been broken into two sections so that it fits within the margins of the page. The continuation is designated by ellipses.)
select * from #MyTA
au_id au_lname au_fname phone contract au_ord royaltyper ----------- ---------- ---------- ------------ -------- ------ ---------- 172-32-1176 White Johnson 408 496-7223 1 1 100 213-46-8915 Green Marjorie 415 986-7020 1 2 40 238-95-7766 Carson Cheryl 415 548-7723 1 1 100 267-41-2394 OLeary Michael 408 286-2428 1 2 40 267-41-2394 OLeary Michael 408 286-2428 1 2 30 274-80-9391 Straight Dean 415 834-2919 1 1 100 409-56-7008 Bennet Abraham 415 658-9932 1 1 60 427-17-2319 Dull Ann 415 836-7128 1 1 50 472-27-2349 Gringlesby Burt 707 938-6445 1 3 30 486-29-1786 Locksley Charlene 415 585-4620 1 1 100 486-29-1786 Locksley Charlene 415 585-4620 1 1 100 672-71-3249 Yokomoto Akiko 415 935-4228 1 1 40 724-80-9391 MacFeather Stearns 415 354-7128 1 1 60 724-80-9391 MacFeather Stearns 415 354-7128 1 2 25 756-30-7391 Karsen Livia 415 534-9219 1 1 75 846-92-7186 Hunter Sheryl 415 836-7128 1 2 50 PART TWO OF THE TABLE CONTENTS title_id title type price advance royalty ---------- ------------------ ------------- ------ -------- ------- PS3333 Prolonged Data Depri psychology 19.99 2000.00 10 BU1032 The Busy Executives business 19.99 5000.00 10 BU2075 You Can Combat Compu business 2.99 10125.00 24 PC1035 But Is It User Frien popular_comp 22.95 7000.00 16 BU1111 Cooking with Compute business 11.95 5000.00 10 TC7777 Sushi, Anyone? trad_cook 14.99 8000.00 10 BU7832 Straight Talk About business 19.99 5000.00 10 BU1032 The Busy Executives business 19.99 5000.00 10 PC8888 Secrets of Silicon V popular_comp 20.00 8000.00 10 TC7777 Sushi, Anyone? trad_cook 14.99 8000.00 10 PC9999 Net Etiquette popular_comp (null) (null) (null) PS7777 Emotional Security: psychology 7.99 4000.00 10 TC7777 Sushi, Anyone? trad_cook 14.99 8000.00 10 BU1111 Cooking with Compute business 11.95 5000.00 10 PS1372 Computer Phobic AND psychology 21.59 7000.00 10 PS1372 Computer Phobic AND psychology 21.59 7000.00 10 PC8888 Secrets of Silicon V popular_comp 20.00 8000.00 10 (17 row(s) affected)
Using SELECT INTO, I created a new table with columns and data from titles, authors, and titleauthor. You can use any legal SELECT syntax with the SELECT INTO, so summary tables can be created by performing aggregates and GROUP BY. In this example, the new table inherits the column names of the old columns because I did not specify column aliases during the SELECT INTO.
Because local temp tables cant be shared among users, MS SQL 6.x came up with the global temporary table. These tables are like local temp tables, but they allow all users on the system to access the same table. To create a global temp table, use two pound signs:
create table ##t1(c1 int not null)
Global temporary tables last until the connection that created them goes away. If another user is using the global table when the parent connection is severed, the global table hangs around until everyone has stopped using it, then goes away.
The phrase stops using it, however, is taken very literally. If you are working with a global temp table and are selecting from it, as soon as the server has finished retrieving the necessary information, the table is considered unused. You cant take a moment to reflect on the results and issue another query, because the table will be gone.
The moral of the story is, if you use global temp tables to share information, make sure that the connection that created the table stays around as long as you want the table to stay around.
Previous | Table of Contents | Next |