Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
43 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 66

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 1000 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 27

Assisted Solution

by:Zberteoc
Zberteoc earned 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

877 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