datetime century date into pad dynamic cursor money percent sp job isnumeric isdate over update
SQLUSA.com
SQL 2008 GRAND SLAM ON 49 CD
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL Server Training SQL 2005 Scripts SQL 2008 Articles
SQL JOBS News Format Developer
How to create a global temporay table?

Execute the following Microsoft SQL Server T-SQL example script in Management Studio Query Editor to create the global temporary table ##Washington with SELECT INTO.

A global temporary table is visible to any session (connection on server) while in existence. It lasts until all users that are referencing the table disconnect.

A local temporary table, like #California below, is visible only the local session (connection) and child session created by dynamic SQL (sp_executeSQL). The temporary table is localized by an auto-generated suffix which is added to the table name. It is deleted after the user disconnects.

In some applications global temporary tables offer better performance than local or table variables.

-- Create global temporary table with select into - SQL select into create table

USE AdventureWorks;

SELECT c.LastName, c.FirstName, a.City, s.StateProvinceCode

INTO ##Washington

FROM Person.Contact c JOIN HumanResources.Employee e

      ON c.ContactID = e.ContactID

INNER JOIN HumanResources.EmployeeAddress ea

      ON e.EmployeeID = ea.EmployeeID

INNER JOIN Person.Address a

      ON ea.AddressID = a.AddressID

INNER JOIN Person.StateProvince s

      ON a.StateProvinceID = s.StateProvinceID

WHERE s.StateProvinceCode = 'WA';

GO

 

SELECT TOP (5) * FROM ##Washington

ORDER BY NEWID()

GO

 

/* Results

 

LastName    FirstName   City        StateProvinceCode

Ford        Jeffrey     Monroe      WA

Zwilling    Michael     Edmonds     WA

Tibbott     Diane       Kenmore     WA

Harrington  Mark        Issaquah    WA

Nay         Lorraine    Edmonds     WA

*/

 

-- Cleanup

DROP TABLE ##Washington

GO

 

-- Create local temporary table with select into - SQL select into create table

USE AdventureWorks;

SELECT c.LastName, c.FirstName, a.City, s.StateProvinceCode

INTO #California

FROM Person.Contact c JOIN HumanResources.Employee e

      ON c.ContactID = e.ContactID

INNER JOIN HumanResources.EmployeeAddress ea

      ON e.EmployeeID = ea.EmployeeID

INNER JOIN Person.Address a

      ON ea.AddressID = a.AddressID

INNER JOIN Person.StateProvince s

      ON a.StateProvinceID = s.StateProvinceID

WHERE s.StateProvinceCode = 'CA';

GO

 

SELECT TOP (5) * FROM #California

ORDER BY NEWID()

GO

 

/* Results

 

LastName    FirstName   City              StateProvinceCode

Raheem      Michael     San Francisco     CA

Ito         Shu         San Francisco     CA

*/

 

-- Cleanup

DROP TABLE #California

GO


Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
Microsoft SQL Server 2012 Training Videos at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Accounting
Administrative
Advertising
Arts
Architecture
Banking
Business Intelligence
Career Jobs
Celebrity
Computer
Consulting
Customer Service
Education
Engineering
Entertainment
Entry Level
Executive
Federal
Finance
Government
Hardware
Healthcare
Hospital
Human Resources
Information Technology
Insurance
Internet
Job Openings
Laboratory
Law Enforcement
Legal
Logistics
Manufacturing
Marketing
Medical
Military
Nursing
Pharmaceutical
Physician
Public Relations
Publishing
Real Estate
Restaurant
Retail
Sales
Social Media
Software
SQL Database
Telecomm
Therapist
Training
Transportation
Truck Driver
Travel
Web
Work from Home

FREE SS SQL / BI OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011 Microsoft Community Contributor 2012

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts

JOIN US ON TWITTER

Copyright 2005-2012, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.