I'm working in a dynamic query where i need to add a column to the query if the parameter value is false.
//SQL:
SET @FilterExp = 'SELECT * from tblName where ptblnFlag = '+Convert(varchar(2),@blnFlag)+') as tbl where 1=1'+ COALESCE(NULLIF( CONVERT(varchar(8000), @FilterExp),''), '')
In the above query i need to check if the @blnFlag
is false then the column can be added in the where condition. If true it need not to be there.
Something like if the condition is false it should return all values and if true the result set is based on the condition.
You just need AND/OR
operator's no need of Dynamic query
here
SELECT *
FROM tblName
WHERE ( ptblnFlag = @blnFlag
AND @blnFlag = 0 )
OR @blnFlag = 1
May be am wrong with variable names but logic will be same
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments