© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
10 replies
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)
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?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Can't use | operator in case?
C#CC# / help
2y ago
How can i make email confirmation 1 in sql server
C#CC# / help
2y ago
Any case where `TError` can't be boxed in example?
C#CC# / help
16mo ago