SQL Server select based on specific condition -


i exclude records having userid null if there @ least 1 record has value in it.

if exists (select * @mysavedsearches userid not null)     select ssm.*      xyztable ssm     join @mysavedsearches mysearches on ssm.uniqueid = mysearches.uniqueid , mysearches.userid not null else     select ssm.*      xyztable  ssm     join @mysavedsearches mysearches on ssm.uniqueid = mysearches.uniqueid 

with above query able desired result, somehow feel it's not correct way.

get count variable , check inside where:

declare @cnt int select @cnt = count(*) @mysavedsearches userid not null  select ssm.*      xyztable ssm     join @mysavedsearches mysearches on ssm.uniqueid = mysearches.uniqueid               , ((@cnt > 0 , mysearches.userid not null) or (@cnt = 0))