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 Order By
SQL ORDER BY Keyword The ORDER BY keyword is used to sort the result-set. The SQL ORDER BY Keyword The ORDER BY keyword is used to sort the result-set by one or more columns. The ORDER BY keyword sorts...

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...

SQL And or Operator
SQL AND & OR Operators The AND & OR operators are used to filter records based on more than one condition. The SQL AND & OR Operators The AND operator displays a record if both the first...

Cursors In SQL Server
First thing first Usage of Cursors is not encouraged in SQL Server as they are slow. You may go with While loop if you need to iterate through a recordset. Cursor is a database object to retrieve data...

SQL Wildcards
SQL Wildcards A wildcard character can be used to substitute for any other character(s) in a string. SQL Wildcard Characters In SQL, wildcard characters are used with the SQL LIKE operator. SQL wildcards...

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