sql server - SQL Use Declare to Omit Where Clause Items -


is there way exclude parts of sql statement based on declared values?

for instance;

declare @onlyy varchar(1) = 'y'  select count(*) main idate > '2016-01-01'   if @onlyy = 'y'     , qualify = 'y' end if 

in case if @onlyy isn't y part in between if/endif wouldn't happen @ all.

the reason need because porting access 97 application .net. in access 97 app there part creates temporary table , generates report table. sql involved huge set of if/then statements remove data temporary table. i'm able build datatable viewing in datagridview. issue can't ssrs have same flexibility .net in if/then statements.

so how should go doing this?

one option can dynamic sql in can dynamically change need.

 declare @sql nvarchar(max)  set @sql = n'select count(*) main idate > ''2016-01-01'''   if @onlyy = 'y'  begin     set @sql += n' , qualify = ''y'''  end   execute sp_executesql @sql