?
Solved

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

Posted on 2014-03-13
5
Medium Priority
?
2,008 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 11

Assisted Solution

by:John_Vidmar
John_Vidmar earned 664 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 668 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 668 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

765 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