Solved

Dynamic sql statement to create table

Posted on 2013-10-24
1
210 Views
Last Modified: 2013-10-24
Where whould I put the INTO tbl_UTILIZATION into the following statement?


Declare @strSQL varchar(max)            


DECLARE @cols varchar(2000)

-- Assigning Y here just for demo purposes, populate however you want. 
Declare @UTILIZED char(1) = 'Y'       

SELECT  @cols = STUFF(( SELECT DISTINCT             
                                '],[' + cast(Level3 as varchar)             
                        From PPS_SKU_UTILIZATION     
                        ORDER BY 1            
                        FOR XML PATH('')            
                      ), 1, 2, '') + ']'            
             
             
set @strSQL = 'select YEAR, MONTH, ' + @cols +           
' from (           
        select YEAR, MONTH, Level3, UTILIZED     
        from PPS_SKU_UTILIZATION
        WHERE UTILIZED=''' + @UTILIZED + ''') o           
pivot (COUNT(UTILIZED) for Level3 in (' + @cols + '))p
ORDER BY MONTH' 
 
exec(@strSQL)   

Open in new window

0
Comment
Question by:Fairfield
1 Comment
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39597733
To create the table every time, to include a fail-safe DROP-CREATE in case the table already exists..
Declare @strSQL varchar(max)            


DECLARE @cols varchar(2000)

-- Assigning Y here just for demo purposes, populate however you want. 
Declare @UTILIZED char(1) = 'Y'       

SELECT  @cols = STUFF(( SELECT DISTINCT             
                                '],[' + cast(Level3 as varchar)             
                        From PPS_SKU_UTILIZATION     
                        ORDER BY 1            
                        FOR XML PATH('')            
                      ), 1, 2, '') + ']'            
             
IF OBJECT_ID('tbl_UTILIZATION') IS NOT NULL
   DROP TABLE tbl_UTILIZATION

set @strSQL = 'select YEAR, MONTH, ' + @cols +           
' INTO tbl_UTILIZATION
from (           
        select YEAR, MONTH, Level3, UTILIZED     
        from PPS_SKU_UTILIZATION
        WHERE UTILIZED=''' + @UTILIZED + ''') o           
pivot (COUNT(UTILIZED) for Level3 in (' + @cols + '))p
ORDER BY MONTH' 
 
exec(@strSQL) 

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach 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 I will describe the Backup & Restore 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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 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

13 Experts available now in Live!

Get 1:1 Help Now