Solved

Where statement in dynamic sql statement

Posted on 2013-10-24
8
304 Views
Last Modified: 2013-10-24
I have the following code in a dynamic sql statement.  I need some help where to put a "where" statement.  I only want to include where the UTILIZED = Y.  Where should I include this parameter in the query?

Declare @strSQL varchar(max)            
DECLARE @cols varchar(2000)            
      
          
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
        
             
) o           
pivot (COUNT(UTILIZED) for Level3 in (' + @cols + '))p' 
 
exec(@strSQL) 

Open in new window

0
Comment
Question by:Fairfield
  • 5
  • 3
8 Comments
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
<Knee-jerk reaction>If you want it as a parameter..
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' 
 
exec(@strSQL) 

Open in new window

0
 

Author Comment

by:Fairfield
Comment Utility
If I wanted to ORDER BY Month, where does that go in this statement?
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
Comment Utility
At the very end of your SQL Statement.   See T-SQL Pivot.
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
 

Author Comment

by:Fairfield
Comment Utility
Lastly I would like to create a table with the results, where do I put the INTO statement?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
>Where should I include this parameter in the query?
>If I wanted to ORDER BY Month, where does that go in this statement?
>Lastly I would like to create a table with the results

Not that we won't answer your question, but does this freight train of a thread have a caboose at the end?   Standard EE question asking protocol is to state your question as clearly as possibe in the beginning, and not string it along by asking an unknown number of follow-on questions.
0
 

Author Comment

by:Fairfield
Comment Utility
This is the last thing I need for this issue, a table created from this query
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
Since this new question is very different from the original, it would be fair to ask that you accept this question, then ask the create table follow-on as a new question.
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
Although at first glance it should be possible by adding the INTO line below
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 + 
INTO YourNewTableName           
' 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

Title # Comments Views Activity
Stored procedure with a parameter 6 19
SQL Server R2 Stored procedure make faster 7 31
SQL Help - 12 32
Row-Level Security 2 14
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 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