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
36 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
ID: 41744319
<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 70

Accepted Solution

by:
Éric Moreau earned 250 total points (awarded by participants)
ID: 41744334
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)
ID: 41744654
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 70

Expert Comment

by:Éric Moreau
ID: 41768446
both comments are valid
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query Optimization 14 44
Simple SQL query from two tables 13 54
Can > be used for a Text field 6 44
PROPERCASE SCRIPT IN SQL 3 7
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

773 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