LOCAL TEMPORARY TABLE in SQL Server

This tutorial explains how to use LOCAL TEMPORARY TABLE in SQL Server with syntax and examples.

This tutorial explains how to use LOCAL TEMPORARY TABLE in SQL Server with syntax and examples.

The local temporary table LOCAL TEMPORARY TABLE is stored in tempdb and SQL Server will automatically delete the table when it is no longer used.

Syntax

 CREATE TABLE #ten_ bang 
(
cot1 kieudulieu [ NULL | NOT NULL ],
cot2 kieudulieu [ NULL | NOT NULL ],

);

Variable name or variable value

ten_bang

The name of the local temp table wants to be created, starting with the # character.

cot1, cot2

The column you want to create in the table. Each column has 1 data type, must be specified to contain NULL or NOT NULL values, otherwise default is NULL.

Data types in SQL Server

Note

The local temp table LOCAL TEMPORARY TABLE has a prefix of # (for example, need #nhanvien).

For example

 CREATE TABL E #nhanvien 
( id_nhanvien INT PRIMARY KEY,
ho (VARCHAR(50) NOT NULL,
ten (VARCHAR(50),
luong MONEY,
);

For example, the CREATE TABLE command will create a local temporary table named #nhanvien with 4 columns:

  1. nhanvien, INT data type, does not contain NULL value.
  2. cough, VARCHAR data type (maximum length of 50 characters) and contains no NULL value.
  3. ten, VARCHAR data type, can contain NULL values.
  4. , MONEY data type, can contain NULL values.
  5. Primary key PRIMARY KEY for table ## nhanvien is id_nhanvien.

The # table is stored in tempdb and SQL Server will be automatically deleted when the SQL Server session is no longer needed.

Previous post: GLOBAL TEMPORARY TABLE in SQL Server

The following article: Foreign Key foreign key in SQL Server

4 ★ | 1 Vote