Hi,
I am not getting the concept of global temporary table in sql server, and how can we create it ?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
SenthilkumarRank #387 · 6.5K reputationPosted Apr 18, 2012, 6:30 PM EDT
For example,
SELECT * INTO ##tmpemp FROM employees
Now the all sql server connection sessions can use this ##tmpemp table.
For more info:
http://www.sqlines.com/articles/sql-server/local_and_global_temporary_tables
Satyapriya NayakRank #47 · 31.5K reputationPosted Apr 18, 2012, 7:11 PM EDT
Global temporary table:- Global temporary table is created in the tempdb and it is visible to all other sessions as well. Global temporary tables are only destroyed when the current user disconnected and all the sessions referring to it closed. It means no user sessions refers it. Since the global temporary table is created in the tempdb, whenever we use temporary tables there is a interaction between the two database (tempdb and the database in which block of code is written) which may slow down the performance. Global temporary table can be used in Joins. Global table is created with the help of the ##.
The syntax for creating the global temporary table is given below:
create table ##[table name] (col1 datatype, col2 datatype, ......coln datatype)
For example, the syntax to create the global temporary table ##employeedetails is given below:
CREATE table ##employeedetails (id int identity(1,1), empFname nvarchar(100), empEname nvarchar(100), empdate datetime)
Create index indx_GTT on ##employeedetails (empFname)
We can also define constraints on the Global temporary tables. For Example
Alter table ##employeedetails add constraint pk_GTT primary key(id)
Insert into ## employeedetails ( empFname , empEname , empdate )
Values ( 'Vivek', ' Johari', getdate())
We can access the global table within the stored procedure in which it is defined and also in the stored procedure which is called inside this stored procedure.
Create procedure test_GTT
as
begin
CREATE table ##employeedetails (id int identity(1,1), empFname nvarchar(100), empEname nvarchar(100), empdate datetime)
exec insert_GTT
select * from ##employeedetails
drop table ##employeedetails
end
Create procedure insert_GTT
as
begin
Insert into ##employeedetails ( empFname , empEname , empdate )
Values ( 'Vivek', ' Johari', getdate())
end
In the above example, we create a global temporary table ##employeedetails
in the stored procedure test_GTT. This procedure call another procedure insert_GTT. The procedure contains the Insert command for the table ##employeedetails. When we execute the procedure test_GTT, it will give the following result.
The global temporary tables can viewed with the help of the system view 'sys.objects'.
SELECT * FROM sys.objects where type='U'
Transactions are also applicable on the Global temporary tables.
Alter procedure test_GTT
as
begin
begin transaction
CREATE table ##employeedetails (id int identity(1,1), empFname nvarchar(100), empEname nvarchar(100), empdate datetime)
exec insert_GTT
rollback
select * from ##employeedetails
drop table ##employeedetails
end
If we execute the above procedure the it will give the following output.
Please refer the below link
http://www.sqlservercentral.com/blogs/vivekssqlnotes/2012/01/07/sql-server-global-temporary-tables/
Thanks