C#C
C#4y ago
FR

❔ SQL Server | Can i use 'Case When' in Where Clause?

i ususally do it by create the query into the string like this
set @sql = 'select * from person p .....'
set @where = ' where xxx = xxx'

IF @role <> 'ALL'
BEGIN
  SET @where= @where + ' AND p.role IN ('''+@ProductId+''')'
END

SET @query = @sql +  @where
EXECUTE(@query)

but it realyyyy hard to debug if the query is very long,
so anyone know the proper way to do it?
Was this page helpful?