Inline Conditional Statement in Stored Procedure

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2010-04-06T20:57:20Z Indexed on 2010/04/06 21:03 UTC
Read the original article Hit count: 249

Here is the pseudo-code for my inline query in my code:

select columnOne
from myTable
where columnOne = '#variableOne#'
  if len(variableTwo) gt 0
      and columnTwo = '#variableTwo#'
  end

I would like to move this into a stored procedure but am having trouble building the query correctly. I assume it would be something like

select columnOne
from myTable
where columnOne = @variableOne
  CASE
    WHEN len(@variableTwo) <> 0 THEN and columnTwo = @variableTwo
  END

This is giving me a syntax error.

Could someone tell me what I've got wrong.

Also, I would like to keep it to only one query and not just have one if statement. Also, I do not want to build the sql in the stored procedure and run Exec() on it.

© Stack Overflow or respective owner

Related posts about tsql

Related posts about sql-server-2005