-> In SQL Server, temporary tables are created at run-time and you can do all the operations which you can do on a normal table. These tables are created inside Tempdb database. Based on the scope and behavior temporary tables are of two types
1) Local Temp Table (#)
2) Global Temp Table (##)
1) Local Temp Table
-> Local temp tables are only available to the SQL server session or connection that created that tables. There are automatically deleted when the session that created the tables has been closed. Local temporary table name is stored with single hash(#) sign
1 2 3 4 5 6 7 8 9 10 |
CREATE TABLE #LocalTemp ( UserID int, Name varchar(50), Address varchar(150) ) GO insert into #LocalTemp values ( 1, 'Shailendra','Noida'); GO Select * from #LocalTemp |
2) Global Temp Table
-> Global temp tables are available to all SQL server sessions or connetions (means to all users). These can be created by any SQL server connection user and these are automatically deleted when all the SQL server connections have been closed.Global temporary table name is started with double hash (##) sign.
1 2 3 4 5 6 7 8 9 10 |
CREATE TABLE ##GlobalTemp ( UserID int, Name varchar(50), Address varchar(150) ) GO insert into ##GlobalTemp values ( 1, 'Shailendra','Noida'); GO Select * from ##GlobalTemp |