Solved

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

Posted on 2014-03-13
5
1,963 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:
ScottPletcher 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now