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
38 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 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

Independent Software Vendors: 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

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.
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

740 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