sql server - Adding an IF Condition in WHERE Clause to Add an 'AND' only on particular occasion. In other occasion skip the 'AND' part -


i'm trying workout this. it's sample code, since i'm not able put whole stored procedure.

i'm taking value table, while getting @children parameter sp. want run and condition if the value in @children greater one. possible add if condition in where clause within sql server 2008? how can achieve following? logically, below situation want achieve. if have questions please let me know, can provide additional information.

select empid, empdesignation, empsalary, haschildren employee empdesignation = 'manager'  if (@children > 2 ) { , haschildren = 1 } 

you can use combined query and , or operators:

select empid, empdesignation, empsalary, haschildren  employee  empdesignation = 'manager'      , (@children < 2 or haschildren = 1) 

in case if @children 1 or less condition @children < 2 valid condition haschildren = 1 never evaluated.

in on other hand @children more 1 condition @children < 2 invalid , in case condition haschildren = 1 evaluated.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -