Introduction

In SQL Server, there are many options to store the data temporarily, which are Temp Table, Table variable, and CTE(Common Table Expressions). In this article, we are going to learn about Temp Table, Table variable, and CTE in SQL Server.

Temp Table (Temporary Table)

Local Temp Table

create table #Student
(
    Id int,
    Name nvarchar(50), 
    Address nvarchar(150)
)
go
insert into #Student values ( 1, 'Test','Tamil Nadu');
go
select * from #Student;

When to use Local Temp Table?

Global Temp Table

create table ##Student
(
    Id int,
    Name nvarchar(50), 
    Address nvarchar(150)
)
go
insert into ##Student values ( 1, 'Test','Tamil Nadu');
go
select * from ##Student;

When to use Global Temp Table?

Temp Variable

When to use Table Variable?

CTE (Common Table Expression)

When to use CTE?

Summary

In this article, you have learned about Temp table, Temp variable, and CTE with examples.