Introduction
Temporary tables are tables which are created at run time. Can perform all kind of operations on temp tables like normal table but scope is limited. These are stored in temporary tables.
Temporary tables are two types:
- Local Temp Tables
- Global Temp Tables
Local Temp Tables:
These are accessed with current connection only. These will delete when user disconnected with instance and prefixed with '#' sign.
Global Variables:
These are accessed with any connection. These will delete when all connections are closed and prefixed with '##' sign.
Example for Temp Tables:
- CREATE TABLE #MYTABLE
- (DID INT,
- DNAME VARCHAR(10),
- DLOC VARCHAR(10)
- )
- INSERT INTO @TEMPTABLEVARIABLE (DID,DNAME) VALUES (14,'CSE','1ST FLOOR')
- SELECT * FROM #MYTABLE
- UPDATE #MTTABLE SET DNAME = 'ECE', DLOC = '4TH FLOOR' WHERE DID = 14
- DELETE FROM #MYTABLE WHERE DID = 14
These are also like temp tables, can be created and can perform all kind of operations like normal tables.
- BEGIN
- DECLARE @TEMPTABLEVARIABLE TABLE (DID INT,DNAME VARCHAR(10),DLOC VARCHAR(100))
- INSERT INTO @TEMPTABLEVARIABLE (DID,DNAME,DLOC) VALUES (14,'CSE','3RD FLOOR')
- INSERT INTO @TEMPTABLEVARIABLE (DID,DNAME,DLOC) VALUES (15,'IT','1ST FLOOR')
- SELECT * FROM @TEMPTABLEVARIABLE
- UPDATE @TEMPTABLEVARIABLE SET DLOC = '5TH FLOOR' WHERE DID = 14
- SELECT * FROM @TEMPTABLEVARIABLE
- DELETE FROM @TEMPTABLEVARIABLE WHERE DID = 14
- SELECT * FROM @TEMPTABLEVARIABLE
- END

Join the conversation! Your thoughts help the community grow.