Friday, 3 August 2018

SQL Server Temprary Table

Temporary table are tables created to hold data temporarily for a particular session. When a create table script is executed for a temporary table, physical table is created on tempdb database. You can see the tables as follows

Object Explorer -> Databases -> System Databases-> tempdb -> Temporary Tables











temporary table can be used as a physical table, but it exist till the session is active who is created the table. Multiple user can create temporary table with same name and data. As each table is session based, table does not conflict  for different user.When
session is expire or connection is broken, temporary table vanish automatically. Here is the syntax of creating temporary table.

Syntax

CREATE TABLE #table_name
(
  column1 datatype NULL/NOT NULL,
  column2 datatype NULL/NOT NULL,
  column3 datatype NULL/NOT NULL,
  ...
);



Data manipulation operation like insert ,update, delete can be done with temporary tables. Below is the example of creation and data manipulation using temporary table.


Create Table

create table #myTable
(
    ID int,
    Name Varchar(50),
    Roll Varchar(50)
)


Create Table and Insert data
create table #myTable
(
ID int,
Name Varchar(50),
Roll Varchar(50)
)

GO

INSERT INTO #myTable(ID,Name,Roll)
SELECT 1,'Student1',123
UNION
SELECT 2,'Student2',124
UNION
SELECT 3,'Student1',125

Output
(3 row(s) affected)











Create Table and Update data 


create table #myTable
(
ID int,
Name Varchar(50),
Roll Varchar(50)
)

GO

INSERT INTO #myTable(ID,Name,Roll)
SELECT 1,'Student1',123
UNION
SELECT 2,'Student2',124
UNION
SELECT 3,'Student1',125

GO
UPDATE #myTable
SET Name='New Srudent'
WHERE ID=1

SELECT * FROM #myTable

Output
ID    Name         Roll
1     New Srudent  123
2     Student2     124
3     Student1     125


Create Table and Delete data


create table #myTable
(
ID int,
Name Varchar(50),
Roll Varchar(50)
)

GO

INSERT INTO #myTable(ID,Name,Roll)
SELECT 1,'Student1',123
UNION
SELECT 2,'Student2',124
UNION
SELECT 3,'Student1',125

GO
DELETE FROM #myTable
WHERE ID=1
GO
SELECT * FROM #myTable


Output
ID    Name         Roll
2     Student2     124
3     Student1     125

A temporary table can we join with other physical or temporary table or table variables as regular tables. Below is the joining example of temporary table with other tables.



create table #myTable
(
ID int,
Name Varchar(50),
Roll Varchar(50)
)


create table student
(
ID int,
Name Varchar(50),
Roll Varchar(50)
)

SELECT a.Name,b.Name
FROM student a
INNER JOIN #myTable b
ON a.ID=b.ID


Constrain can be define in  Temporary table. Primary key, foreign key ,identity ,default value , checking can be defined. Below example of temporary people with identity and primary key and Default value.




create table #myTable
(
     ID int identity primary key ,
     Name Varchar(50),
     Roll Varchar(50),
     createtime datetime DEFAULT CURRENT_TIMESTAMP
)

Temporary table can be used in stored procedure. Here is a example of temporary table is used in stored procedure.


CREATE PROCEDURE sp_my_procedure
            @LastName nvarchar(50),
            @FirstName nvarchar(50)
AS

create table #myTable
(
         ID int identity primary key ,
         Name Varchar(50),
         Roll Varchar(50),
         createtime datetime DEFAULT CURRENT_TIMESTAMP
)


create table student
(
        ID int,
        Name Varchar(50),
        Roll Varchar(50)
)

SELECT a.Name,b.Name
FROM student a
INNER JOIN #myTable b
ON a.ID=b.ID

GO


Temporary table cannot be used in function. Temporary table is used to hold large data ,Temporary table can hold large data, with fast response.


CREATE FUNCTION dbo.fn_GetName()
RETURNS int
AS
BEGIN
 
DECLARE @ret int;
 
create table #myTable
(
      ID int identity primary key ,
      Name Varchar(50),
      Roll Varchar(50),
      createtime datetime DEFAULT CURRENT_TIMESTAMP
)
RETURN @ret;
END;

Msg 2772, Level 16, State 1, Procedure fn_GetName, Line 8
Cannot access temporary tables from within a function.

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...