Solved

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

Posted on 2014-01-07
4
552 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 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
AND:

INSERT INTO #tp EXEC sp_executesql @sql
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 0 total points
Comment Utility
>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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now