Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-01-07
4
Medium Priority
?
587 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
  • 2
4 Comments
 
LVL 9

Assisted Solution

by:s_chilkury
s_chilkury earned 400 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 200 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 400 total points
ID: 39763038
AND:

INSERT INTO #tp EXEC sp_executesql @sql
0
 
LVL 66

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

886 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