SQL Query Combine with string

  • Hello!

            I would like to know one thing : how to combine condition string with query statement.

    eg.

    IF @Value = 0

         Set @STR = 'AND ID = 100'

    ELSE

          Set @STR = 'AND ID = 200'

    SELECT ID,Name,Description

    FROM Table1

    WHERE Name = 'AA'  & @STR

    I want to use something like that? Please help me....

     

  • Something like this should cut it for you:

    Declare @SQL as varchar(200)

    Declare @Value int

    Set @SQL = 'SELECT ID, Name, Description FROM Table1 WHERE Name = ''A'''

    IF @Value = 0

     BEGIN

      Set @SQL = @SQL + ' AND ID = 100'

     END

    ELSE

     BEGIN

      Set @SQL = @SQL + ' AND ID = 200'

     END

    execute(@SQL)

  • Actually, it can be done without the overhead of Dynamic SQL by using a CASE statement in the WHERE clause....

     SELECT ID,Name,Description 

       FROM Table1

      WHERE Name = 'AA'

        AND ID = CASE

                     WHEN @Value = 0 THEN 100

                     ELSE 200

                  END

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Dear Grasshopper,

            Thanks for your answer but my query statement is long, over 255 characters. So I have to seperate many strings, isn't it?

    Best Regards,

    MK.

  • Morean,

    SellerTools' answer can easily be modified to handle 8000 characters just by changing "Declare @SQL as varchar(200)" to "Declare @SQL as varchar(8000)".  Still, it's dynamic SQL and might be better off adding the CASE statement to the WHERE clause... and then you don't have to worry about such things, either.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • hey grasshopper,

    I agree with Jeff that more than likely, you are better off making a smarter statement rather than resorting to dynamic SQL. I think the only time I personally have had to use dynamic SQL was in the creation of tables. I tdoes have its places, but with the example that you gave, either a CASE statement would work, or even better would be something like;

    Create Procedure dbo.MyProcedure

    (

    @Value as int

    )

    Declare @IDValue as int

    if @Value=0

      BEGIN

        Set @IDValue = 100

      End

    Else

      BEGIN

        SET @IDValue = 200

      END

    BEGIN

    SELECT

      ID,

      Name,

      Description

    FROM Table1 WHERE Name = 'AA' AND ID = @IDValue

    END

    GO

  • Grasshopper,

             Thanks a lot!

    MK.

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

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