SQL server short circuit problem

The SQL server optimizer may rearrange the order of the logical expressions. In this case the short circuit evaluation is not going to work.

For example, you may have logical expressions (A and B). But, the optimizer may rearrange it to (B and A).

The work around is the CASE statement.

CASE
   WHEN NOT A THEN 0
   WHEN NOT B THEN 0
   ELSE 1
END = 1

For information about short circuits, see the T-SQL Black Belt column “Short Circuit,” September 2000, InstantDoc ID 9148 @ sqlmag.com

http://www.sqlmag.com/Articles/ArticleID/9148/pg/2/2.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s