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
39 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 27

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

734 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