Adding column in where condtion dynamically

  • Hi,

    I have following tables

    product

    customer

    agent

    we have column by name "state code" in product and customer.

    i have sql like

    select * from table name(we get table name dynamically like product,customer and agent using variable)

    i need to add condition to get records with statecode=0

    when i use where condition in the above query it looks like

    select * from table name(we get table name dynamically like product,customer and agent)

    where statecode=0

    but this dynamic query fails for table "agent" as agent table does not contain statecode column.

    so how can i wirte query where i get "where state code=0" condtion only for product and customer but not to agent.

    mean

    for product and customer query should look like this

    select * from table name(we get table name dynamically like product,customer and agent)

    where statecode=0

    for agent :

    select * from table name(we get table name dynamically like product,customer and agent)

    i have tried

    if (variable='product' or variable='customer')

    select * from table name(we get table name dynamically like product,customer and agent)

    where statecode=0

    else

    select * from table name(we get table name dynamically like product,customer and agent)

    am looking at, is there any other way to implement this?

  • You can implement this with Dynamic SQL as well

    DECLARE@TableName VARCHAR(100), @SQL VARCHAR(1000)

    SET@TableName = 'agent'

    SET@SQL= ' SELECT* '

    + ' FROM ' + @TableName

    + ' WHERE ' + CASE WHEN @TableName IN ( 'product', 'customer' ) THEN 'statecode = 0' ELSE '1=1' END

    EXECUTE ( @SQL )


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Hi Kingston Dhasian,

    i implemented as per your inputs it is working as expected.

    thank you!!!

  • You would be better served with the last line like this:

    EXEC sp_executesql @SQL

    You should have a higher plan reuse rate instead of the standard EXECUTE (@SQL)

  • Or even better create 3 stored procs to retrieve your data. You are using the "one query fits all via dynamic sql" approach. This is full of issues. It is difficult to maintain, once business rules start diverging you have really complicated code (this sounds the case here), you are running the risk of sql injection (maybe not now but in the future).

    The code that Kingston shared will work but breaking this into separate pieces is really the best move in the long run.

    _______________________________________________________________

    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/

  • DiverKas (1/15/2013)


    You would be better served with the last line like this:

    EXEC sp_executesql @SQL

    You should have a higher plan reuse rate instead of the standard EXECUTE (@SQL)

    AND it allows for parameterization which is vital if using dynamic sql.

    _______________________________________________________________

    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/

  • I strongly recommend against this approach. It's a security risk and a maintenance nightmare. You wouldn't develop a C# method that could handle customers or products depending on a parameter passed, don't do it to SQL procedures.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply