Solved

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

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

861 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

29 Experts available now in Live!

Get 1:1 Help Now