Solved

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

Posted on 2014-01-07
4
573 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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

635 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