Click here to Skip to main content
15,913,133 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

How to insert second table data into temporary table when two tables as a result from stored proceedure.

Sample Code:

Create table #temp(number int)

insert into #temp(number)

exec proc_testing

where as proc_testing retruns two tables as result set .

i want to insert second table result from the result in to temporary table

What I have tried:

Create table #temp(number int)

insert into #temp(number)

exec proc_testing
Posted
Updated 5-May-16 1:49am
v2
Comments
Tomas Takac 5-May-16 3:52am    
Not clear. You should post your code. The "What I have tried" section is intended just for that.
Baroor 5-May-16 4:27am    
Sample Code:

Create table #temp(number int)

insert into #temp(number)

exec proc_testing

where as proc_testing retruns two tables as result set .

i want to insert second table result from the result in to temporary table

1 solution

The second table will be inserted into #temp with the code you have.

Note that this will only work if both record sets have matching columns.

For example (using Microsoft's Northwind database)
SQL
CREATE PROCEDURE [dbo].[GetAllNames] 
AS 
    SELECT ContactName FROM dbo.Customers
    SELECT FirstName + ' ' + LastName FROM dbo.Employees
The first query returns 91 records and the second returns 9.

If I run the SP like this
SQL
create table #temp([name] varchar(125))

insert into #temp
exec GetAllNames

and then issue
SQL
select * from #temp
100 records will be returned.

If I change the SP
SQL
ALTER PROCEDURE [dbo].[GetAllNames] 
AS 
    SELECT ContactName FROM dbo.Customers
    SELECT FirstName,LastName FROM dbo.Employees -- note 2 columns
and attempt to run it I get an error
Quote:
Msg 213, Level 16, State 7, Procedure GetAllNames, Line 11
Column name or number of supplied values does not match table definition.

You can't access the resultsets separately in T-SQL, but you can load the results into a DataSet in C# or VB.NET and access the tables separately that way
 
Share this answer
 
v2
Comments
Baroor 5-May-16 8:11am    
Thanks @Chill60
CHill60 5-May-16 8:13am    
My pleasure

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900