Solved

How to result of a stored procedure into temp table without having to define temp table lay out first

Posted on 2016-08-05
4
32 Views
Last Modified: 2016-08-24
I was using the following code in one of my sql server 2012 stored procedures



IF OBJECT_ID('tempdb..#PlayerDataWithoutADTFunctionData') IS NOT NULL
      DROP TABLE #PlayerDataWithoutADTFunctionData

----Add all 24KT, Sterling, Bronze regardlessof whether they had visits with play in the period or not
SELECT @Campaignid AS CampaignID
      ,pd.PlayerId
      ,@PlayerPlayRangeBeginDate AS PlayerPlayRangeBeginDate
      ,@PlayerPlayRangeEndDate AS PlayerPlayRangeEndDate
      ,g.RankingID AS 'Tier'
      ,MAX(pd.AccountingDate) AS 'LastPlay'
      ,isnull(SUM(pd.Slot_CoinIn) - SUM(pd.Slot_CoinOut) - SUM(pd.Slot_Jackpot),0) AS 'SlotWin6Month'
      ,isnull(SUM(pd.Table_Win),0) AS 'TableWin6Month'
      ,isnull(COUNT(DISTINCT pd.AccountingDate),0) AS 'Trips6Month'
      ,isnull(SUM(pd.Slot_TheoWin),0) AS 'SlotTheo6Month'
      ,isnull(SUM(pd.Table_TheoWin),0) AS 'TableTheo6Month'
      ,isnull(SUM(pd.Slot_TheoWin) + SUM(Table_TheoWin),0) AS 'TotalTheo6Month'
      ,isnull((SUM(pd.Slot_CoinIn) - SUM(pd.Slot_CoinOut) - SUM(pd.Slot_Jackpot)) + SUM(pd.Table_Win) / COUNT(DISTINCT AccountingDate),0) AS 'ADW6Month'
      ,isnull((SUM(pd.Slot_TheoWin) + SUM(pd.Table_TheoWin)) / COUNT(DISTINCT AccountingDate),0) AS 'ADT6Month'
INTO #PlayerDataWithoutADTFunctionData
FROM CopyPlayerManagement.dbo.PlayerDay AS pd


One of my coworkers wrote a stored proc that will return  the  above fields (and more) from our data warehouse tables that has
pre aggregated data     (using startdate and enddate as input parameters)


I would like to make use of the stored procedure but would prefer not to have to create the actual layout of the temp table first as this stored proc will have additional fields added to it that I won't need for my purposes and I don't want to have to keep modifying layout of temp table

Is there a way to dump results of stored procedure into temp table without having to create layout of temp table first?????

(like the code above does)
0
Comment
Question by:johnnyg123
  • 2
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
<very wild guess>

SELECT * INTO YourTableName FROM exec YourStoredProc

Not sure I'd recommend this approach, as there could be downstream errors that cannot be predicted with an unknown schema.
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 250 total points (awarded by participants)
Comment Utility
select into from a SP just doesn't work!

But you can use the OpenQuery as shown in http://www.sommarskog.se/share_data.html
0
 
LVL 26

Assisted Solution

by:Zberteoc
Zberteoc earned 250 total points (awarded by participants)
Comment Utility
This is how you can do it:
SELECT 
    *
    into #temp		
FROM 
    OPENROWSET('SQLNCLI', 'server=(local);trusted_connection=yes', 
	   'set fmtonly off; EXEC DatabaseName.dbo.StoreProcedureName @param_string=''string_value_here'', @param_date=''YYYY-MM-DD'';')

Open in new window

With parameters I just wanted to exemplify how you do it and that you have to use 2 single quotes to wrap the string values. You cannot use variables for that. You would have to dynamically built the whole script and then execute it.
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
both comments are valid
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 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