• rs80 (10/8/2013)


    Guess my question wasn't clear?

    I'm not clear on it. So far you've given us your control table. If you could give us that but instead as DDL and consumable sample data, it would be a start. Example:

    DECLARE @control TABLE

    (

    TableName VARCHAR(20)

    ,ColumnName VARCHAR(20)

    ,ColumnValue VARCHAR(100)

    ,IncludeExclude VARCHAR(10)

    );

    INSERT INTO @control

    SELECT 'tblProduct','ProductID','1','Exclude'

    UNION ALL SELECT 'tblProduct','ProductID','2','Exclude'

    UNION ALL SELECT 'tblProduct','ProductName','Keyboard','Include'

    UNION ALL SELECT 'tblProduct','ProductName','Mouse','Include'

    UNION ALL SELECT 'tblOrder','OrderID','1','Exclude'

    UNION ALL SELECT 'tblOrder','OrderID','2','Exclude'

    UNION ALL SELECT 'tblOrder','OrderName','PackageA','Exclude'

    UNION ALL SELECT 'tblOrder','OrderName','PackageB','Exclude'

    SELECT * FROM @control

    Now you also should provide the DDL for tbProducts and tbOrders, along with some consumable sample data.

    Finally, we need to see what your expected results are.

    You probably don't need a CURSOR. You may need Dynamic SQL although that's not for certain.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St