Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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…

808 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