Solved

Where statement in dynamic sql

Posted on 2013-10-23
2
223 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 year = 2013 and the month = 3.  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
2 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 39595771
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
        WHERE [year] = 2013 and [month] = 3
             
) o           
pivot (COUNT(UTILIZED) for Level3 in (' + @cols + '))p' 
 
exec(@strSQL)

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39596634
Sweaty Feet Will Give Horrible Odours

Select
From
Where
Group by
Having
Order by
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQl query 19 19
SQL Maintenance Plan 3 29
SQL Transaction logs 8 26
Sql Server 2008 R2 Stored Procedure doesn't work with CTE and parameters 4 31
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…
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

947 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

23 Experts available now in Live!

Get 1:1 Help Now