When we develop an application we often need to provide temporary table functionality in our application.
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 is for a master table with an id as the primary key. The other table was a Transaction table that has the foreign key. The master table id should be transfered to the transaction table.
We initially create 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 create a temporary table on MasterTable.
- 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 that the original data in the temporary table uses 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 or intermediate results 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 create 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 check the 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
-
-