Solved

Dynamic sql statement to create table

Posted on 2013-10-24
1
215 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
[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
1 Comment
 
LVL 66

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

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

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