• thbaig (10/1/2015)


    This is the only thing left.

    Case-1

    DECLARE @ProdId varchar(10) = 'Prod1',

    @color varchar(max) = NULL, -- Will make it optional and logic will work on all available color for Product.

    @size varchar(max) = 'L,S,M,MM'; -- Will consider only provided sizes

    Case-2

    DECLARE @ProdId varchar(10) = 'Prod1',

    @color varchar(max) = 'Black,Blue,bbc' -- Will consider only provided colors

    @size varchar(max) = NULL, -- Will make it optional and logic will work on all available sizes for product

    Case-3

    DECLARE @ProdId varchar(10) = 'Prod1',

    @color varchar(max) = NULL, -- Will make it optional and logic will work on all available color for Product.

    @size varchar(max) = NULL, -- Will make it optional and logic will work on all available sizes for product

    Here is how I would deal with this. I would create a main procedure that receives all your parameters. Then a procedure for each possible path. So for example in Case-1 you would call the procedure that can handle that situation. The code would be very similar to when all parameters are passed except that you would just use a cross join to the ProductColor instead of splitting the parameter. Repeat this for each of your 3 new cases. Case-4 would be the code I already posted. Then you just need a procedure as the traffic controller. Sure you could do that all in a single procedure but you are going to have some serious performance problems. This article from Gail explains the performance benefits of this approach. http://sqlinthewild.co.za/index.php/2009/09/15/multiple-execution-paths/[/url]

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/