Skip to content
Advertisement

Trying to update where clause to only apply to a certain value

I have a stored procedure in my database. It is a dynamic piece of code and runs for multiple different parameters.

Let’s say I have a parameter called @Param that gets passed: 17 (this parameter will be different for different processes).

In my query I am attempting to use this parameter to update the WHERE clause like this:

SELECT val1, val2 
FROM tbl1
WHERE 1 = 1 
  AND ((val2 IN ('h1', 'h2') AND @Param IN (17)) OR @Param NOT IN (17))

Does code like this make sense? I’m trying to only apply this WHERE clause: (val2 in (‘h1′,’h2’) to @Param=17

Advertisement

Answer

You seems to want :

where (@param = 17 and val2 in ('h1', 'h2')) or
      (@param <> 17)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement