4

I'm debugging some code and came across this. Could someone help me by putting parens around this statement according to SQL Server ordering. Is it just me, or is this bad coding?

WHERE 
T1.C1 = @VAR1 AND T1.C2 = @VAR2 AND T1.C3 NOT IN(@VAR3)
OR T2.C1 = @VAR2 OR T3.C1 = @VAR2
000
  • 26,951
  • 10
  • 71
  • 101
mwill
  • 424
  • 7
  • 21
  • 1
    We can't help you because we don't know what you mean. You surround the clauses you want considered together using `(parentheses)`. So, if you mean `(a and b) and (c or d or e)` then write that, if you mean `(a and b and c) or (d and e)` then write that. – Aaron Bertrand Jun 13 '13 at 18:41
  • PS `AND` has higher precedence. So if you write `a and b or c` then you get `(a and b) or (c)`. If you meant `(a) and (b or c)` then you need to write it that way. – Aaron Bertrand Jun 13 '13 at 18:44
  • let us know which statements/conditions you want to test first. – Sunil B N Jun 13 '13 at 18:46
  • I'm asking how will the compiler parse it. I assume it will parse it the same every time as it is. I would like the parens added for readability *according to how the parser would execute it if thee were no parens". – mwill Jun 13 '13 at 18:46

1 Answers1

5

As is, it will be parsed like

WHERE 
(T1.C1 = @VAR1 AND T1.C2 = @VAR2 AND T1.C3 NOT IN(@VAR3))
OR
(T2.C1 = @VAR2)
OR
(T3.C1 = @VAR2)
000
  • 26,951
  • 10
  • 71
  • 101