Solved

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

Posted on 2014-03-13
5
1,991 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 41

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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