rather unhelpfully re-orders for me. I was wondering if anyone agrees
that the way it's done it is correct or not?
My original code is...
select a.person_refno
from tblPersons a join tblPersonStats b on a.person_refno =
b.person_refno
where a.Forename = 'John'
and not (b.Code1 = 'AA' and b.Code2 = 'BB' and b.Code3 = 'CC')
...which is converted by Enterprise Manager to...
select a.person_refno
from tblPersons a join tblPersonStats b on a.person_refno =
b.person_refno
where (a.Forename = 'John') and (not(b.Code1 = 'AA')) or
(a.Forename = 'John') and (not(b.Code2 = 'BB')) or
(a.Forename = 'John') and (not(b.Code3 = 'CC'))
Now, I could live with this, IF it had placed extra brackets around
each of the 'OR'ed' sets of statements, e.g. something like
.. ((field1 = x) and (not(field2=y))) OR ((field1 = z) and
(not(field2=y))) ..
Does anyone know if the way EM's done it is OK, i.e. does each 'OR'
automatically get recognized as a separate 'branch'...I always thought
that mixing ANDs and ORs all over the place without wider brackets or
parentheses was bad practice and was liable to confuse the processing!Just to add, I have run a few tests on this using my syntax and
Enterprise Manager's modified syntax (through Query Analyzer) and it
does seem to produce the same results...however, if anyone could
confirm that this mixing of ANDs and ORs without parentheses, that
Enterprise Manager seems to prefer, is still definitely OK as T-SQL
syntax - that would be great.|||It is logically the same, but I always put parenthesis around my criteria
when using ORs to make it perfectly clear what I am trying to do. It is way
too easy to forget the precedence and end up with the wrong data if you do
not explicitly group everything.
That, and I sometimes get

forgetting that AND precedes OR.
Using carriage returns and indenting also help make it more readable for the
next time the code has to be modified.
<champ.supernova@.gmail.com> wrote in message
news:1146062160.096159.44870@.y43g2000cwc.googlegroups.com...
> Just to add, I have run a few tests on this using my syntax and
> Enterprise Manager's modified syntax (through Query Analyzer) and it
> does seem to produce the same results...however, if anyone could
> confirm that this mixing of ANDs and ORs without parentheses, that
> Enterprise Manager seems to prefer, is still definitely OK as T-SQL
> syntax - that would be great.
>|||Thanks Jim, I normally would also add the extra brackets, but EM
doesn't seem to want any of it. I guess the application knows best in
this case!|||The easiest way to prevent EM from messing up your queries is not to let him
near them. :) S

any T-SQL development).
ML
http://milambda.blogspot.com/|||IMO, EM is lousy for writing code. I use it for convenience sometimes, but
I never use the code it generates (rewrites) becuase I find it poorly
formatted for the most part. I usually restructure the code when I am done
playing in EM and run it in query analyzer if I need to create any objects,
that way my formatting gets preserved.
<champ.supernova@.gmail.com> wrote in message
news:1146063626.862473.313330@.j33g2000cwa.googlegroups.com...
> Thanks Jim, I normally would also add the extra brackets, but EM
> doesn't seem to want any of it. I guess the application knows best in
> this case!
>
No comments:
Post a Comment