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.
  1. Create table MasterTable
  2. (
  3. Id int identity(1, 1),
  4. Name varchar(50)
  5. )
  6. Create table TransactionTable
  7. (
  8. t_id int identity(1, 1),
  9. fk_Id int, (Foreign key)
  10. )
Then we create a temporary table on MasterTable.
  1. Create table#tempTest
  2. (
  3. Cid int identity(1, 1),
  4. Id int,
  5. Name varchar(50)
  6. )
  7. Insert into#tempTest(Id, Name)
  8. select Id, Name from MasterTable
Now you can see that the original data in the temporary table uses a select statement.
  1. select * from #tempTest
When to use a temporary table

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 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.
  1. Declare @var int=1
  2. While @var <= (Select COUNT(*) from #tempTest)
  3. Begin
  4. Declare @tempid int
  5. Select @tempid = Id from #tempTest where Cid=@var
  6. select @tempid
  7. Insert into MasterTable(Name)
  8. Select name from MasterTable where Id=@tempid
  9. --select * from #tempTest
  10. set @var=@var+1
  11. Drop table #tempTest
  12. 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.
  1. Declare @identity int
  2. set @identity =@@IDENTITY
  3. --select @identity
  4. Insert into TransactionTable(fk_Id)
  5. Select @identity from Acct_Test2 where fk_id=@tempid
The following is the Stored Procedure to do that.
  1. Create Procedure Procedurename
  2. As
  3. Create table #tempTest
  4. (
  5. Cid int identity (1,1),
  6. Id int,
  7. Name varchar(50)
  8. )
  9. Insert into #tempTest(Id,Name)
  10. select Id, Name from MasterTable
  11. --select COUNT(*) from #tempTest
  12. select * from #tempTest
  13. Declare @var int=1
  14. While @var <= (Select COUNT(*) from #tempTest)
  15. Begin
  16. Declare @tempid int
  17. Select @tempid = Id from #tempTest where Cid=@var
  18. select @tempid
  19. Insert into MasterTable(Name)
  20. Select name from MasterTable where Id=@tempid
  21. --select * from #tempTest
  22. Declare @identity int
  23. set @identity =@@IDENTITY
  24. --select @identity -76
  25. Insert into TransactionTable(fk_Id)
  26. Select @identity from TransactionTable where fk_id=@tempid
  27. set @var=@var+1
  28. Drop table #tempTest
  29. END
  30. --SELECT * FROM [RTU_Test].[dbo].[MasterTable]
  31. --Select fk_id from TransactionTable