UrbanPro
true

Learn MS SQL Development from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Temp Table in SQL Server

Amitava Majumder
08/03/2017 0 0

Temp Table in SQL Server

The temp table in SQL Server can be created at the run-time, and perform all the operation that a normal table can do. There are two types of Temporary Tables in SQL Server: Local Temporary Tables, and Global Temporary Tables. In this article we will show you, How to work with both Local, and Global Temp table in SQL.

Before we get into the examples, following list will show you, where we can use the SQL temp tables:

  • When we are working with the complex joins.
  • Temp tables are useful to replace the costly cursors. We can use this temp table to store the result set data, and then we can manipulate the data from the temp table.
  • We can use this, when we are doing large number of row manipulation in stored procedures. Remember, If we create a temp table inside the stored procedure, then it will be applicable to that SP only. It means, you can not call the temp table from outside the stored procedure.

Local Temp Table in SQL Server

The name of the Local temporary table starts with the hash (“#”) symbol, and it is stored in the tempdb. Local temporary tables are available only in the current connection. If the user disconnects from current instances, then they are automatically deleted. Or, If we close the Query Window then the table will be dropped.

Local Temp Table in SQL Server Syntax

The following code snippet will show you the syntax behind the local temporary tables in SQL

CREATE TABLE #[Local Temp Table]

(

    Column_Name1 Data_Type(Size) [NULL | NOT NULL],

    Column_Name2 Data_Type(Size) [NULL | NOT NULL],

    Column_NameN Data_Type(Size) [NULL | NOT NULL]

);

 

Here, Table Name of a local temporary table should start with #. Remember, Please provide Unique table name here. If you write the already existing table name, it will throw an error

Create Local Temp Table in SQL Server Example

We are going to use the below shown code to create local temporary table called LocalTemp.

SQL CODE

-- Creating Local Temp Table in SQL Server

CREATE TABLE #LocalTemp

(

[ID] [int] IDENTITY(1,1) NOT NULL,

[FirstName] [nvarchar](255) NULL,

[LastName] [nvarchar](255) NULL,

[Occupation] [nvarchar](255) NULL,

[YearlyIncome] [float] NULL,

[Sales] [float] NULL

)

 

From the above code you can observe that, We declared 6 Columns. Here, Our first column is ID of Integer data type, and it will not allow NULL values. We also defined this column as Identity starting with 1 and incremented by 1.

OUTPUT

From the above screenshot you can observe that, Command is executed successfully. Please refresh the object explorer to see the Newly created Temporary Local table

Insert Data into Local Temp Table in SQL Server

Let me insert few random, or sample records into the local temporary table that we created inside the tempdb using the INSERT Statement.

SQL CODE

-- Inserting Values into Local SQL Temp Table

INSERT INTO #LocalTemp (

    [FirstName], [LastName], [Occupation], [YearlyIncome], [Sales])

VALUES ('Bob', 'Ward', 'Finance', 20000, 300)

      ,('Anil', 'Jain', 'Software Professional', 30000, 200)

      ,('Scott', 'Ford', 'Trainer', 25000, 40)

      ,('Amit', 'Kumar', 'Hr Executive', 35000, 890)

 

OUTPUT

Select Data from Local Temp Table in SQL Server

From the above screenshot you can see that, we successfully inserted 4 random records into the #LocalTemp table. Let me use the SELECT Statement to select the records present in the temp table.

SQL CODE

-- Selecting Values From Local SQL Temp Table

SELECT [FirstName],

       [LastName],

&

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

SQL Statements
SQL SELECT Statement The SELECT statement is used to select data from a database. The SQL SELECT Statement The SELECT statement is used to select data from a database. The result is stored in a result...

SQL Joins
SQL Joins SQL joins are used to combine rows from two or more tables. SQL JOIN An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them. The most common...

10 Study Tips to Achieve your Goals
This is particularly true of students that are looking to make the most of their study time and get better grades at Organization. 1. Set Study Goals: There is lots of credible research suggesting that...

Understanding Indexes In SQL Server
Indexes in SQL Server are created on columns in tables or views. The index provides a faster way to look up data based on the values in those columns.For example, if you create an index on the primary...

What's the best way to learn SQL Server?
Just read the book, type and run the code, and work the chapter examples. If you work the book, you will be ahead of many SQL Server programmers. You’ll understand declarative set-based interaction...

Looking for MS SQL Development Training?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for MS SQL Development Classes?

The best tutors for MS SQL Development Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn MS SQL Development with the Best Tutors

The best Tutors for MS SQL Development Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more