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)
- )
- 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
If 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 --select * from #tempTest
- set
- @var = @var + 1
- Drop table #tempTest
- END
- Declare @identity int
- set @identity =@@IDENTITY
- --select @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 COUNT(*) from #tempTest
- 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
- --select * from #tempTest
- Declare @identity int
- set @identity =@@IDENTITY
- --select @identity -76
- Insert into TransactionTable(fk_Id)
- Select @identity from TransactionTable where fk_id=@tempid
- set @var=@var+1
- Drop table #tempTest
- END
- --SELECT * FROM [RTU_Test].[dbo].[MasterTable]
- --Select fk_id from TransactionTable

SubashPosted Aug 25, 2016, 12:36 AM
Good
Prashant VermaPosted Mar 10, 2016, 3:22 AM
Nice
SharadPosted Jul 22, 2015, 12:30 AM
good one...
Karthik Muthu KaruppanPosted Apr 15, 2015, 12:59 PM
Nice one
Sibeesh VenuPosted Apr 15, 2015, 8:36 AM
Nice
NitinPosted Apr 15, 2015, 2:52 AM
nice
Khargesh RajputPosted Apr 15, 2015, 12:32 AM
nice sir
Rajeev RanjanPosted Apr 14, 2015, 11:01 PM
That's great @Rohtas sir
Santhakumar MunuswamyPosted Apr 14, 2015, 10:48 PM
Thanks for Good one