Executing SQL Command???

  • Sorry guys, but I have a real simple question that I can't figure out. I'm playing around with this bit of code for now and I can't seem to get it to execute.

    DECLARE @filterCriteria varchar

    DECLARE @filterCMD varchar

    set @filterCriteria = 'w'

    IF @filterCriteria LIKE 'W'

    set @filterCMD=' DESCRIP LIKE ''%closed%'' or DESCRIP LIKE ''%deny%'' or DESCRIP like ''%withdrawn%'' '

    select accountID from TEMP_TABLE WHERE + @filterCMD

    SQL Server doesn't like the way I try to concat @filterCMD and says incorrect syntax error. What am I doing wrong?

  • Hey jmarg,

    You would have to resort to dynamic SQL to be able to concatenate @filterCMD.

    From looking at the code posted, you could do this:

    IF @filterCriteria LIKE 'w'

      SELECT accountID FROM TEMP_TABLE WHERE DESCRIP LIKE '%closed%' or DESCRIP LIKE '%deny%' or DESCRIP LIKE '%withdrawn%'

    JP

  • There is no problem with the set statement where @FilterCmd is concatenated. The problem is in the final select statement. The WHERE clause is incorrect.

  • Hi,

    Try the code given below.

    DECLARE @filterCriteria varchar

    DECLARE @filterCMD varchar

    declare @sql1 nvarchar(200)

    set @filterCriteria = 'w'

    IF @filterCriteria LIKE 'W'

    set @filterCMD=' DESCRIP LIKE ''%closed%'' or DESCRIP LIKE ''%deny%'' or DESCRIP like ''%withdrawn%'' '

    set @sql1='select accountID from TEMP_TABLE WHERE ' + @filterCMD

    exec sp_executesql @sql1

    Hope this solves the issue.

    Nivedita Sundar.N

Viewing 4 posts - 1 through 3 (of 3 total)

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