[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

4.6

Removing a dynamic query

Asked by dotnetpro in SQL Server 2005, MS SQL Server

Tags: SQL

Experts i am trying to remove a dynamic query.Please see the attachment for the original and changed query


Original Query:

 
alter procedure [dbo].[usp_Inventory_Detail_MainHeadings]        
@PO_Number varchar(256),        
@PO_Type varchar(256),        
@Vendor varchar(256),        
@Label varchar(256),        
@Gender varchar(256),        
@Season varchar(256),        
@Category varchar(256),        
@Classification varchar(256),        
@Description varchar(256),        
@Style varchar(256),        
@Quality varchar(256),        
@Color varchar(256),        
@Size varchar(256),        
@Type varchar(256),        
@Units_Greater_Than integer,        
@Excess_Inventory varchar(1),        
@Cost_Per_Unit_Over  money,        
@Cost_Per_Unit_Under money,        
@Warehouse varchar(256),        
@Section varchar(256),        
@Units_Less_Than integer,        
@Status varchar(256),        
@SO_Number varchar(256)  
   
AS        
 
 
declare @reporting_job_id bigint  
declare @stmt varchar(8000)        
declare @stmt1 varchar(8000)        
declare @fields varchar(2000)        
 
 
 
 
   
 

   
if (@Size <> '_SKIP_')        
begin        
select        
  distinct '(['+size.description+'_units'+'] > 0) and ' as field,        
  space(1) as RecList        
 into        
  #workingtable        
 from size, iter_intlist_to_table(@Size) i_size where        
 --quantity_size.size_id = size.size_id and        
 size.size_id = i_size.number        
       
 set @fields = ''        
 select        
  @fields=@fields+field        
 from        
  #workingtable        
           
end        
         
set @stmt = ''        
set @stmt1 = ''        
       
--set @stmt='select distinct ''Section: ''+LTRIM(STR(section_number))+''   ''+section_header+'' Inventory'''        
set @stmt='select distinct ''Section: ''+LTRIM(STR(section_number))+''   ''+section_header+'' Inventory'''        
set @stmt= @stmt+' as "MainHeading" '        
set @stmt= @stmt+'from '        
set @stmt= @stmt+'wms_reports_staging_table  '        
if (@Size <> '_SKIP_')        
begin        
 set @stmt= @stmt+', size '        
 set @stmt= @stmt+',iter_intlist_to_table('''+@Size+''') i_size '        
end        
if @Label <> '_SKIP_'        
    set @stmt1 = @stmt1 + ',iter_intlist_to_table('''+@Label+''') i_label '        
if @Classification <> '_SKIP_'        
    set @stmt1 = @stmt1 + ',iter_intlist_to_table('''+@Classification+''') i_class '        
if @Vendor <> '_SKIP_'            
     set @stmt1 = @stmt1 + ',iter_intlist_to_table('''+@Vendor+''') i_vendor '        
if @Season  <> '_SKIP_'        
    set @stmt1 = @stmt1 + ',iter_intlist_to_table('''+@Season+''') i_season '        
if @Gender <> '_SKIP_'            
     set @stmt1 = @stmt1 + ',iter_intlist_to_table('''+@Gender+''') i_gender '        
if @Category  <> '_SKIP_'            
    set @stmt1 = @stmt1 + ',iter_intlist_to_table('''+@Category+''') i_category '        
if @Status  <> '_SKIP_'            
    set @stmt1 = @stmt1 + ',iter_intlist_to_table('''+@Status+''') i_status '            
if @Description  <> '_SKIP_'            
    set @stmt1 = @stmt1 + ',iter_intlist_to_table('''+@Description+''') i_carton'        
if @Quality  <> '_SKIP_'            
    set @stmt1 = @stmt1 + ',iter_intlist_to_table('''+@Quality+''') i_quality '        
if @Color  <> '_SKIP_'            
    set @stmt1 = @stmt1 + ',iter_intlist_to_table('''+@Color+''') i_color'        
if @Warehouse  <> '_SKIP_'            
    set @stmt1 = @stmt1 + ',iter_intlist_to_table('''+@Warehouse+''') i_warehouse '        
if @Style  <> '_SKIP_'            
    set @stmt1 = @stmt1 + ', iter_intlist_to_table('''+@Style+''') i_style '        
if @PO_Type <> '_SKIP_'            
    set @stmt1 = @stmt1 + ', iter_intlist_to_table('''+@PO_Type+''') i_po_type '        
if @PO_Number <> '_SKIP_'        
    set @stmt1 = @stmt1 + ',iter_charlist_to_table('''+@PO_Number+''') i_po_number '        
if @SO_Number <> '_SKIP_'        
    set @stmt1 = @stmt1 + ',iter_charlist_to_table('''+@SO_Number+''') i_so_number '        
if @Section <> '_SKIP_'            
    set @stmt1 = @stmt1 + ', iter_intlist_to_table('''+@Section+''') i_section '        
if @Type <> '_SKIP_'            
    set @stmt1 = @stmt1 + ', iter_charlist_to_table('''+@Type+''') i_type '        
           
       
--set @stmt1 = @stmt1 + ' where inventory_status = ''Active'' '        
set @stmt1 = @stmt1 + ' where '        
if (@Size <> '_SKIP_')        
begin        
 set @stmt1 = @stmt1 + @fields        
 set @stmt1 = @stmt1 + ' size.size_scale_id = wms_reports_staging_table.size_scale_id and size.size_id = i_size.number and '        
end        
set @stmt1 = @stmt1 + ' 1 = 1 '        
if @Status <> '_SKIP_'        
 set @stmt1 = @stmt1 + ' and (inventory_status_id = i_status.number) '        
if @PO_Number <> '_SKIP_'        
 set @stmt1 = @stmt1 + ' and (po_number = i_po_number.item) '        
if @SO_Number <> '_SKIP_'        
 set @stmt1 = @stmt1 + ' and (so_number = i_so_number.item) '        
if @PO_Type <> '_SKIP_'        
 set @stmt1 = @stmt1 + ' and (po_type_id = i_po_type.number) '        
if @Section <> '_SKIP_'        
 set @stmt1 = @stmt1 + ' and (section_number = i_section.number) '        
if @Type <> '_SKIP_'        
 set @stmt1 = @stmt1 + ' and (case when type='''' then ''nej'' when type is null then ''nej'' else type end = i_type.item) '        
       
if @Vendor <> '_SKIP_'        
 set @stmt1 = @stmt1 + ' and (vendor_id = i_vendor.number) '        
if @Label <> '_SKIP_'        
 set @stmt1 = @stmt1 + 'and (label_id = i_label.number) '        
if @Gender <> '_SKIP_'        
 set @stmt1 = @stmt1 + 'and (gender_id = i_gender.number) '        
if @Season  <> '_SKIP_'        
 set @stmt1 = @stmt1 + 'and (season_id = i_season.number) '        
if @Classification  <> '_SKIP_'        
 set @stmt1 = @stmt1 + 'and (classification_id = i_class.number) '        
if @Category  <> '_SKIP_'        
 set @stmt1 = @stmt1 + 'and (category_id = i_category.number)  '        
if @Description  <> '_SKIP_'        
 set @stmt1 = @stmt1 + 'and (label_description_id = i_carton.number) '        
if @Style  <> '_SKIP_'        
 set @stmt1 = @stmt1 + 'and (style_id = i_style.number) '        
if @Quality  <> '_SKIP_'        
 set @stmt1 = @stmt1 + 'and (quality_id = i_quality.number) '        
if @Color  <> '_SKIP_'        
 set @stmt1 = @stmt1 + 'and (color_id = i_color.number) '        
if @Warehouse  <> '_SKIP_'        
set @stmt1 = @stmt1 + 'and (warehouse_id = i_warehouse.number) '        
--if (@Units_Greater_Than <> 0 )        
--    set @stmt1 = @stmt1 + 'and units > '+ str(@Units_Greater_Than)        
--if (@Units_Less_Than <> 0 )        
--    set @stmt1 = @stmt1 + 'and units < '+ str(@Units_Less_Than)        
if (@Cost_Per_Unit_Over <> 0.0 )        
    set @stmt1 = @stmt1 + 'and cost > '+ str(@Cost_Per_Unit_Over)        
if (@Cost_Per_Unit_Under <> 0.0 )        
    set @stmt1 = @stmt1 + 'and cost < '+ str(@Cost_Per_Unit_Under)        
if (@Excess_Inventory = '1')        
    set @stmt1 = @stmt1 + 'and cost = 0'        
set @stmt1= @stmt1+'  GROUP BY '        
set @stmt1= @stmt1+'  section_number, section_header, '        
set @stmt1= @stmt1+' quality_description+char(1)+label_description+char(1)+gender_description+char(1)+season_description+char(1)+classification_description+char(1)+label_description_description+char(1)+category_description '        
set @stmt1= @stmt1+' ,isnull(type, ''''), cost'        
       
set @stmt1= @stmt1+' having (sum(units) > '+str(@Units_Greater_Than)+' or '+str(@Units_Greater_Than)+' = 0)'        
set @stmt1= @stmt1+' and (sum(units) < '+str(@Units_Less_Than)+' or '+str(@Units_Less_Than)+' = 0)'        
       
set @stmt1= @stmt1+' order by ''Section: ''+LTRIM(STR(section_number))+''   ''+section_header+'' Inventory'''        
    print    (@stmt+@stmt1)    
exec (@stmt+@stmt1)    
 

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
Updated Query:
 
declare @PO_Number varchar(256),        
@PO_Type varchar(256),        
@Vendor varchar(256),        
@Label varchar(256),        
@Gender varchar(256),        
@Season varchar(256),        
@Category varchar(256),        
@Classification varchar(256),        
@Description varchar(256),        
@Style varchar(256),        
@Quality varchar(256),        
@Color varchar(256),        
@Size varchar(256),        
@Type varchar(256),        
@Units_Greater_Than integer,        
@Excess_Inventory varchar(1),        
@Cost_Per_Unit_Over  money,        
@Cost_Per_Unit_Under money,        
@Warehouse varchar(256),        
@Section varchar(256),        
@Units_Less_Than integer,        
@Status varchar(256),        
@SO_Number varchar(256),
@fields varchar(2000)   
 
 
select distinct '(['+size.description+'_units'+'] > 0) and ' as field,space(1) as RecList into  #workingtable from size, iter_intlist_to_table(@Size) i_size where size.size_id = i_size.number set @fields = '' select  @fields=@fields+field from #workingtable       
 
select distinct 'Section: '+LTRIM(STR(section_number))+'   '+section_header+' Inventory' as "MainHeading" from 
wms_reports_staging_table
,size, 
iter_intlist_to_table(@Size) i_size,
iter_intlist_to_table(@Label) i_label, 
iter_intlist_to_table(@Classification) i_class, 
iter_intlist_to_table(@Vendor) i_vendor,
iter_intlist_to_table(@Season) i_season, 
iter_intlist_to_table(@Gender) i_gender, 
iter_intlist_to_table(@Category) i_category, 
iter_intlist_to_table(@Status) i_status,
iter_intlist_to_table(@Description) i_carton,
iter_intlist_to_table(@Quality) i_quality,
iter_intlist_to_table(@Color) i_color,
iter_intlist_to_table(@Warehouse) i_warehouse,
iter_intlist_to_table(@Style) i_style,
iter_intlist_to_table(@PO_Type) i_po_type,
iter_charlist_to_table(@PO_Number) i_po_number,
iter_charlist_to_table(@SO_Number) i_so_number,
iter_intlist_to_table(@Section) i_section,
iter_charlist_to_table(@Type) i_type
	
where 
+ (case when @fields<>'' then @fields else '' end)+
 
size.size_scale_id = wms_reports_staging_table.size_scale_id 
and size.size_id = i_size.number 
and 1 = 1 and (inventory_status_id = i_status.number)
and (po_number = i_po_number.item)
and (so_number = i_so_number.item)
and (po_type_id = i_po_type.number)
and (section_number = i_section.number)
and (case when type='' then 'nej' when type is null then 'nej' else type end = i_type.item) 
and (vendor_id = i_vendor.number)
and (label_id = i_label.number)
and (gender_id = i_gender.number)
and (season_id = i_season.number)
and (classification_id = i_class.number)
and (category_id = i_category.number) 
and (label_description_id = i_carton.number) 
and (style_id = i_style.number)
and (quality_id = i_quality.number) 
and (color_id = i_color.number)
and (warehouse_id = i_warehouse.number)
and cost > str(@Cost_Per_Unit_Over)
and cost <  str(@Cost_Per_Unit_Under)
and cost = 0
 
Related Solutions
Keywords: Removing a dynamic query
 
Loading Advertisement...
 
[+][-]10/28/08 08:47 AM, ID: 22823083Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: SQL Server 2005, MS SQL Server
Tags: SQL
Sign Up Now!
Solution Provided By: skrile
Participating Experts: 1
Solution Grade: A
 
[+][-]10/23/08 08:01 AM, ID: 22786850Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/23/08 08:33 AM, ID: 22787285Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/23/08 08:43 AM, ID: 22787394Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/23/08 08:51 AM, ID: 22787510Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/23/08 08:55 AM, ID: 22787560Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/23/08 07:41 PM, ID: 22792863Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/24/08 04:45 AM, ID: 22795186Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/24/08 06:25 AM, ID: 22795913Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/24/08 06:27 AM, ID: 22795931Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/24/08 01:43 PM, ID: 22799947Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/26/08 04:32 PM, ID: 22809273Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/27/08 04:07 AM, ID: 22811295Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/27/08 05:24 AM, ID: 22811839Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/27/08 05:50 AM, ID: 22812007Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/27/08 08:58 AM, ID: 22813843Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/27/08 10:53 AM, ID: 22814942Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/27/08 10:54 AM, ID: 22814955Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/28/08 06:55 AM, ID: 22821755Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/28/08 07:15 AM, ID: 22822007Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/28/08 07:22 AM, ID: 22822083Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/28/08 07:40 AM, ID: 22822272Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/28/08 08:08 AM, ID: 22822621Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/28/08 08:10 AM, ID: 22822645Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/28/08 08:23 AM, ID: 22822799Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/28/08 09:01 AM, ID: 22823224Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_2_20070628