Precedence and Logical Operators

  • I am having the hardest time logically determining what is exactly happending in code shown below. Does anyone have any tricks to breaking this type of thing down?

    IF (@394 = 1 AND (@244 = 0 OR (@244 = 1 AND @395 = 0)) AND ((@225 = 1 AND @183 = 1) OR

    (@225 = 1 AND @183 = 0 AND @396 = 1) OR ((@225 = 0 OR (@225 = 1 AND @183 = 0

    AND @396 = 0)) AND @397 = 1 AND @398 = 1) OR ((@225 = 0 OR (@225 = 1 AND

    @183 = 0 AND @396 = 0)) AND @397 = 1 AND @398 = 0 AND @399 = 1)))

    BEGIN

    Thanks,

    A clever person solves a problem. A wise person avoids it. ~ Einstein
    select cast (0x5365616E204465596F756E67 as varchar(128))

  • This may help a little...

    IF ( @394 = 1

    AND ( @244 = 0

    OR ( @244 = 1

    AND @395 = 0

    )

    )

    AND ( ( @225 = 1

    AND @183 = 1

    )

    OR ( @225 = 1

    AND @183 = 0

    AND @396 = 1

    )

    OR ( ( @225 = 0

    OR ( @225 = 1

    AND @183 = 0

    AND @396 = 0

    )

    )

    AND @397 = 1

    AND @398 = 1

    )

    OR ( ( @225 = 0

    OR ( @225 = 1

    AND @183 = 0

    AND @396 = 0

    )

    )

    AND @397 = 1

    AND @398 = 0

    AND @399 = 1

    )

    )

    )

    Where the hell did that come from?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thank bunches. It is code used within our application I support. It is pretty ugly no doubt but really cool how you broke it down. It boggled my mind. Thanks!

    A clever person solves a problem. A wise person avoids it. ~ Einstein
    select cast (0x5365616E204465596F756E67 as varchar(128))

  • Don't thank me. Redgate's SQL Prompt -> Format SQL. Don't think I would have had the patience to do it by hand.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • And, let me guess, no documentation on the business-logic that created that?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Your right no business logic documented. I have been tasked with doing that.

    A clever person solves a problem. A wise person avoids it. ~ Einstein
    select cast (0x5365616E204465596F756E67 as varchar(128))

  • GSquared (1/13/2012)


    And, let me guess, no documentation on the business-logic that created that?

    Logic? Documentation? Really, what makes you think either of those exists in most companies? 😉

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • SQL Prompt is pretty nifty 🙂

    A clever person solves a problem. A wise person avoids it. ~ Einstein
    select cast (0x5365616E204465596F756E67 as varchar(128))

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply