I want to use where clause in TableName conditional Eg. When my variable @col1 is blank then I dont want to use where with @col1 and it should check for @col2 If @col2 is also blank then return all records. Or else check with their respective conditions/values inside variables.
declare @col1 nvarchar(50)=''
declare @col2 nvarchar(50)=''
select * from TableName
where
(isnull(@col1 ,'')!='' AND col1 =@col1 )
OR
(isnull(@col2 ,'')!='' AND col2 =@col2 )
This returns no record when @col1 or @col2 is blank.
Instead , when both are blank it should return all records, If any one of them contains any value then it should go for that condition.
Edit : Dynamic Sql is not allowed