Description
This article explains temporary tables with example in SQL Server.
When we develop an application, in many cases we need to use temporary tables in our applications.
So let's have a look at a practical example of how to create a Stored Procedure to insert values into multiple tables in SQL Server using a temporary table.
The example is developed in SQL Server 2012 using the SQL Server Management Studio.
The requirement was to use a master table containing an id as the primary key. The other table was a Transaction table with a foreign key.
The master table id should be transferred to the transaction table.
First we have created two tables named MasterTable and TransactionTable.
- Create table MasterTable (
- Id int identity (1, 1),
- Name varchar(50)
- )
- Create table TransactionTable (
- t_id int identity (1, 1),
- fk_Id int,
- (Foreign key)
- )
Then we created a temporary table on the master table.
- Create table #tempTest
- (
- Cid int identity (1, 1),
- Id int,
- Name varchar(50)
- )
- Insert into #tempTest(Id,Name)
- select
- Id,
- Name
- from MasterTable
Now you can see the original data in a temporary table using a select statement.
When to use a temporary tableIf there is logic inside a Stored Procedure that involves manipulation of data that cannot be done within a single query, then in such cases, the output of one query / intermediate result can be stored in a temporary table that then participates in further manipulation via joins and so on to achieve the final result.
Now we need to transfer the primary key id in the transaction table. To do that we created a loop on the master table to visit all the ids one by one.
- Declare @var int = 1 While @var <= ( Select COUNT(*) from #tempTest)
- Begin Declare @tempid int
- Select
- @tempid = Id
- from
- #tempTest where Cid=@var
- select
- @tempid Insert into MasterTable(Name)
- Select name from MasterTable
- where Id = @tempid
- set
- @var = @var + 1
- Drop table #tempTest
- END
Now we need to fetch the master table id and insert into the transaction table. In the master table we have applied an identity with a primary key. So we can fetch the last inserted value from the master table using @@IDENTITY.
- Declare @identity int
- set @identity =@@IDENTITY
-
- Insert into TransactionTable(fk_Id)
- Select @identity from Acct_Test2 where fk_id=@tempid
The following is the Stored Procedure to do that.
- Create Procedure Procedurename
- As
- Create table #tempTest
- (
- Cid int identity (1,1),
- Id int,
- Name varchar(50)
- )
- Insert into #tempTest(Id,Name)
- select Id, Name from MasterTable
-
-
- select * from #tempTest
-
- Declare @var int=1
- While @var <= (Select COUNT(*) from #tempTest)
- Begin
- Declare @tempid int
- Select @tempid = Id from #tempTest where Cid=@var
- select @tempid
-
- Insert into MasterTable(Name)
- Select name from MasterTable where Id=@tempid
-
-
- Declare @identity int
- set @identity =@@IDENTITY
-
-
- Insert into TransactionTable(fk_Id)
- Select @identity from TransactionTable where fk_id=@tempid
-
- set @var=@var+1
- Drop table #tempTest
- END
-
-