Solved

Is there a way to exec SP into a temp table?

Posted on 2014-01-07
4
559 Views
Last Modified: 2014-03-07
Hi all

I'm doing some troubleshooting, and I'd like to insert the return set of a stored procedure into a temp table.   Since it's 75 columns, I'd rather not do a CREATE TABLE and define every column.

Is there a way to 'SELECT * INTO #tmp FROM exec sp_my_proc'?

SQL 2008R2.

Thanks in advance.
Jim
0
Comment
Question by:Jim Horn
[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
  • 2
4 Comments
 
LVL 9

Assisted Solution

by:s_chilkury
s_chilkury earned 100 total points
ID: 39763025
Or do it the following way:

Dump the resultset of the SP - directly to the temp table like:

SELECT * INTO #tp FROM (output sql of SP)
0
 
LVL 12

Assisted Solution

by:Habib Pourfard
Habib Pourfard earned 50 total points
ID: 39763028
try:
INSERT INTO #tmp(col1, ..., colN)
EXEC sp_my_proc

Open in new window

0
 
LVL 9

Assisted Solution

by:s_chilkury
s_chilkury earned 100 total points
ID: 39763038
AND:

INSERT INTO #tp EXEC sp_executesql @sql
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 0 total points
ID: 39763043
>SELECT * INTO #tp FROM (output sql of SP)
Define 'output SQL of SP'.  If you mean copy-paste the T-SQL in the SP, ok...

>INSERT INTO #tmp(col1, ..., colN)
I didn't want to define all 75 columns to pull this off, if possible.

Figured it out with this article.   I'll keep the question alive to solicit more advice, then close and award points for help
sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO

SELECT * INTO #tmp FROM OPENROWSET('SQLNCLI', 'Server=server_name;Trusted_Connection=yes;',
'EXEC database_name.dbo.sp_name ''2010-01-01''')
SELECT *
FROM #tmp

Open in new window

0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

749 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