LOCAL TEMPORARY TABLE in SQL Server
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 TABLE #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:
- nhanvien, INT data type, does not contain NULL value.
- cough, VARCHAR data type (maximum length of 50 characters) and contains no NULL value.
- ten, VARCHAR data type, can contain NULL values.
- , MONEY data type, can contain NULL values.
- 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
You should read it
- GLOBAL TEMPORARY TABLE in SQL Server
- TEMPORARY TABLE temporary table in SQL
- ALIAS in SQL Server
- How to set up a local web server (Local Web Server) on Windows, macOS, and Linux
- CREATE TABLE command in SQL Server
- Segment tables in SQL Server
- DROP TABLE command in SQL Server
- PIVOT clause in SQL Server
- ALTER TABLE statement in SQL Server
- The difference between web server and app server
- DELETE command in SQL Server
- UPDATE command in SQL Server