Labels

Sunday, May 1, 2022

Temp Tables in SQL (Part -8)

As its name indicates, temporary tables are used to store data temporarily and they can perform CRUD (Create, Read, Update, and Delete).

Temp table will not exist once application has been closed.

Temporary tables are dropped when the session that creates the table has closed, or can also be explicitly dropped by users. At the same time, temporary tables can act like physical tables in many ways, which gives us more flexibility. Such as, we can create constraints, indexes, or statistics in these tables. SQL Server provides two types of temporary tables according to their scope:

  • Local Temporary Table
  • Global Temporary Table

The local temporary tables are created in the tempdb database with a unique name because they can be created with the same name by the other connections. In this way, SQL Server prevents the same name conflict.

The global temporary tables are created using the CREATE TABLE statement and their names must be prefixed with the double hashtag (##) sign. These tables can be accessed by all other sessions, unlike local ones.



Now, create a temporary table which has structure similar to production table.


In Back Up, only Schema is copied.


You can also Drop Temp table from table.

Insert Data into another table using Select statement.


No comments:

Post a Comment