Dynamic WHERE Clause within a SQL Stored Procedure


  • I have to build a dynamic WHERE Clause within a SQL Stored Procedure. All of my SQL is native...meaning at this point it is NOT dynamic. I am wondering if I can build my WHERE clause based on the parameters that are provided. Right now I have this...

    IF @GroupNbr IS NOT NULL
    SET @FromDate = NULL;
    SET @ThruDate = NULL;
    SET @SQLWhereString1 = 'WHERE GRPSPAN.GROUP_NBR = ' + '''' + @GroupNbr + '''';

    And then I have in my WHERE clause...

    WHERE @SQLWhereString1

    And that is highlighting with the error...

    "An expression of non-boolean type specified in a context where a condition is expected."

    Any help is greatly appreciated and Thanks in advance for your review and am hopeful for a reply.


    Friday, February 01, 2013 3:12 PM

All replies