Solved

How Do You Insert Values Into a TEMP Table from a Stored Procedure?

Posted on 2014-03-13
5
1,971 Views
Last Modified: 2014-03-14
I am trying to insert values returned from a stored procedure into a temp table.

The stored procedure returns 3 table sets.  I only need the data in the first table set.

Here is my code:
      DECLARE @ReturnResults TABLE(
            ADUserID VARCHAR(10),
            Name VARCHAR(200),
            Standard VARCHAR(8000)
      )

      INSERT
      INTO @ReturnResults
      EXEC [COSSA].[dbo].usp_COSSA_UserManagement_Get_UserAccessForEntity                                                                                                       
      
      SELECT *
      FROM @ReturnResults

Thanks,

Dan
0
Comment
Question by:danielolorenz
5 Comments
 
LVL 11

Assisted Solution

by:John_Vidmar
John_Vidmar earned 166 total points
ID: 39927000
Your technique should work if your table-variable matches your stored-procedure (SP) result-set both in field-name and data-type.

If you are permitted to alter the SP then you can change it to accept a table-variable argument, and populate that in the SP.
0
 
LVL 25

Assisted Solution

by:Lee Savidge
Lee Savidge earned 167 total points
ID: 39927039
Well, firstly you're using a table variable, not a temporary table. There is a difference. One limitation to table variables is that execute can't be used to insert into a table variable.

Similary you can't use select into either. You should look at using proper temporary tables.

http://technet.microsoft.com/en-us/library/ms175010%28v=sql.105%29.aspx
0
 
LVL 40

Expert Comment

by:Sharath
ID: 39927596
>> The stored procedure returns 3 table sets.
If you don't need other 2 table sets, why is it returning? Why don't you change the SP to return one data set only?
0
 

Author Comment

by:danielolorenz
ID: 39927726
I was told I am not allowed to modify the stored procedure to return only 1 table set.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 167 total points
ID: 39927836
I don't believe you can handle that in TSQL.  You'll need to be in a language that will let you handle multiple result sets being returned from the same proc.  AFAIK, TSQL can't do that for you.

Maybe you could add an optional param to the stored proc.  The param would be a temp table name.  When that name is present, add dynamic SQL to the proc code to also insert the first result into the temporary table name you passed in.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question