Sunday, 15 July 2012

sql - IF Inside WHERE with stored procedure -



sql - IF Inside WHERE with stored procedure -

i have statement looks this:

where ((@value1 null , [value1_id] null) or [value1_id] = isnull(@value1, [value1_id])) , ((@value2 null , [value2_id] null) or [value2_id] = isnull(@value2, [value2_id])) , ((@value3 null , [value3_id] null) or [value3_id] = isnull(@value3, [value3_id])) , ((@value4 null , [value4_id] null) or [value4_id] = isnull(@value4, [value4_id])) , ((@value5 null , [value5_id] null) or [value5_id] = isnull(@value5, [value5_id])) , ((@value6 null , [value6_id] null) or [value6_id] = isnull(@value5, [value6_id]))

i need add together conditional logic within of can special things value5 , value6. basically, if value5, value6, or value null, want utilize value 5 , value 6 lines is. if 3 values aren't null, need run calculations on values.

any ideas on best action be?

use case

"case can used in statement or clause allows valid expression. example, can utilize case in statements such select, update, delete , set, , in clauses such select_list, in, where, order by, , having."

http://msdn.microsoft.com/en-us/library/ms181765.aspx

you can perform calculations within of select this:

select case when @value7 not null @value7 * 100 else value7 * 100 end #t (@value1 null , value1 null) , (@value2 null , value2 null)

it's still not clear you're trying accomplish. where not within select? provide clear, concise illustration if above not correct.

sql if-statement null where

No comments:

Post a Comment