Global Temporary table is physical table which is created for
temporary purpose to hold data.Global Temporary table starts with##.
Global Temporary table is not user or session specific. Once a Global
Temporary table is created, the table remain until a drop command is executed
or connection is broken. When a create table script is executed for a Global
Temporary, physical table is created on tempdb database. You can
see the tables as follows
“Object Explorer -> Databases -> System Databases-> tempdb -> Temporary Tables”
A Global Temporary table is visible to all user, a particular user can create a Global Temporary table, but it will be visible to all user. When connection is broken with databases, Global Temporary table disappear automatically.
Here is the syntax of Global Temporary table.
Syntax
CREATE TABLE ##table_name
(
column1 datatype NULL/NOT NULL,
column2 datatype NULL/NOT NULL,
column3 datatype NULL/NOT NULL,
...
);
You can do data manipulation operation like insert, update, delete with Global Temporary table. Below is the example of how a Global Temporary table is created and data manipulation can be done in Global Temporary table.
Create Table
Global Temporary table can be used for joining purpose. Joining operation can be done with physical tables, table variable ect. Below is the example of joining of Global Temporary table.
Global Temporary table can be used in a store procedure. Below is the example of Global Temporary table used in stored procedure.
CREATE PROCEDURE sp_my_procedure
one of the major difference with temp table and Global temporary table. Global temporary table are not user a dependent, it does not depend on session, only depend on connection. Till the connection is active the Global temporary table is available in the database until drop moment is not executed
“Object Explorer -> Databases -> System Databases-> tempdb -> Temporary Tables”
A Global Temporary table is visible to all user, a particular user can create a Global Temporary table, but it will be visible to all user. When connection is broken with databases, Global Temporary table disappear automatically.
Here is the syntax of Global Temporary table.
Syntax
CREATE TABLE ##table_name
(
column1 datatype NULL/NOT NULL,
column2 datatype NULL/NOT NULL,
column3 datatype NULL/NOT NULL,
...
);
You can do data manipulation operation like insert, update, delete with Global Temporary table. Below is the example of how a Global Temporary table is created and data manipulation can be done in Global Temporary table.
Create Table
create
table ##myTable
(
ID
int,
Name
Varchar(50),
Roll
Varchar(50)
)
Create Table
and Insert data
create
table ##myTable
(
ID
int,
Name
Varchar(50),
Roll
Varchar(50)
)
GO
INSERT
INTO
##myTable(ID,Name,Roll)
SELECT
1,'Student1',123
UNION
SELECT
2,'Student2',124
UNION
SELECT
3,'Student1',125
Output
(3
row(s)
affected)
Create Table
and Update data
create
table ##myTable
(
ID
int,
Name
Varchar(50),
Roll
Varchar(50)
)
GO
INSERT
INTO
##myTable(ID,Name,Roll)
SELECT
1,'Student1',123
UNION
SELECT
2,'Student2',124
UNION
SELECT
3,'Student1',125
GO
UPDATE
##myTable
SET
Name='New
Srudent'
WHERE
ID=1
SELECT
* FROM
##myTable
Output
ID
Name Roll
1 New
Srudent 123
2 Student2 124
3 Student1 125
Create Table
and Delete data
create
table
##myTable
(
ID
int,
Name
Varchar(50),
Roll
Varchar(50)
)
GO
INSERT
INTO
#myTable(ID,Name,Roll)
SELECT
1,'Student1',123
UNION
SELECT
2,'Student2',124
UNION
SELECT
3,'Student1',125
GO
DELETE
FROM
##myTable
WHERE
ID=1
GO
SELECT
* FROM
##myTable
Output
ID
Name Roll
2 Student2 124
3 Student1 125
Global Temporary table can be used for joining purpose. Joining operation can be done with physical tables, table variable ect. Below is the example of joining of Global Temporary table.
create
table
##myTable
(
ID
int,
Name
Varchar(50),
Roll
Varchar(50)
)
create
table
student
(
ID
int,
Name
Varchar(50),
Roll
Varchar(50)
)
SELECT
a.Name,b.Name
FROM
student a
INNER
JOIN
##myTable b
ON
a.ID=b.ID
Global Temporary table can be used in a store procedure. Below is the example of Global Temporary table used in stored procedure.
CREATE PROCEDURE sp_my_procedure
@LastName
nvarchar(50),
@FirstName
nvarchar(50)
AS
create
table
##myTable
(
ID
int
identity
primary
key
,
Name
Varchar(50),
Roll
Varchar(50),
createtime
datetime
DEFAULT
CURRENT_TIMESTAMP
)
create
table
student
(
ID
int,
Name
Varchar(50),
Roll
Varchar(50)
)
SELECT
a.Name,b.Name
FROM
student a
INNER
JOIN
##myTable b
ON
a.ID=b.ID
GO
one of the major difference with temp table and Global temporary table. Global temporary table are not user a dependent, it does not depend on session, only depend on connection. Till the connection is active the Global temporary table is available in the database until drop moment is not executed