Home Forums SQL Server 7,2000 T-SQL Any way to clean up mutually exclusive OR conditions? RE: Any way to clean up mutually exclusive OR conditions?

  • xr280xr (9/26/2016)


    I have a where clause with some mutually exclusive OR expressions. With a procedural language that allows short circuiting it would look like this:

    a || b || c

    The important thing is that || b implies a is false and || c implies a and b are false. My understanding is because t-sql may execute them in any order, I would need to declare this explicitly:

    a OR (!a AND b) OR (!a AND !b AND c)

    But when a, b, and c are verbose expressions, this gets really ugly and will be error prone during maintenance. Is there anything beyond formatting that can be done to simplify this?

    That might work in procedural languages, but SQL isn't procedural. It's a bit of a hybrid but is more declarative than procedural.

    Why does this matter? As with most other declarative languages - the SQL engine has latitude over how to answer the query you put together. While there are exceptions, in most cases when you try to play cute and try to specify HOW it operates, performance suffers.... a lot. SQL is designed to operate well on large sets, and most times when you "play", you end up forcing the engine to look at each row one at a time (rather than 100K at a time or faster).

    You REALLY want to verify that things really are slower WITHOUT short-circuiting.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?