SQL Server – Global temporary tables


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 “” not found /]
(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)

We can also create indexes on the Global temporary tables. For example,

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)

Similar to physical table and local temporary table, the values in the global temporary table can also be inserted with the help of the Insert command. For example, to insert the value in the global temporary table ## employeedetails  , we can use the following SQL Statement:

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.

Exec test_GTT

Global temporary tables are itself dropped when the current user session is closed and no other user is referring to it, but it is better to drop it manually at the end of the block of the code in which it is defined or at the end of the stored procedure in which it is defined.

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.

Limitations:-

  1. We can’t use global temporary table inside a SQL Function.
  2. We can’t create triggers on the temporary tables. 
  3. We can’t create View on the temporary tables.

DMCA.com

About vivekjohari

Database Consultant with more than 11.5 years of experience in database designing & programming and DBA related activities.  Had good experience on different databases like SQL Server, MySQL & Oracle, Azure SQL &  Big Data.
This entry was posted in SQL Server, SQL Temporary Tables and tagged , . Bookmark the permalink.

31 Responses to SQL Server – Global temporary tables

  1. Anonymous says:

    we can create view on global temp table ?

  2. Anonymous says:

    Hello SIR,
    can we create function,package,cursor and trigger on global temp table

  3. Anonymous says:

    Hi Vivek,
    it's a quick and useful article. I liked it.

  4. Suresh says:

    Very usefull Thanks bro

  5. rakesh says:

    The last point is wrong you cannot create view based on Temporary Table.

  6. Pingback: Life Insurance in North Hollywood

  7. Pingback: Terry Healy Group Ltd

  8. Pingback: jasaqq

  9. Pingback: http://webuyhouses-il.info/usa/real-estate-investment/il/lincolnwood

  10. Pingback: https://routersettings.site

  11. Pingback: 사설토토사이트 추천

  12. Pingback: 카지노사이트

  13. Pingback: CASINO Online

  14. Pingback: success

  15. Pingback: Bassett Caterers

  16. Pingback: Website

  17. Pingback: Apollon Market

  18. Pingback: casino truc tuyen khuyen mai

  19. Pingback: sahabatqq

  20. Pingback: qiuqiu 99

  21. Pingback: nadapoker

  22. Pingback: tai khoan m88

  23. Pingback: Dentist in Monroeville PA

  24. Pingback: 420 Cash Flow

  25. Pingback: replica watches on the net watches

  26. Pingback: ww88

  27. Pingback: fowling rules

  28. Pingback: have a peek at this web-site

Leave a Reply