SQL Server INTERNALS: IF clause

SQL Server INTERNALS: IF clause

Post by Oz Ben Elieze » Sun, 29 Oct 2000 02:23:48



Hey all,

I'm using MS-SQL Server 2000.

My question is this -
When using an IF clause in a stored procedure - suppose it has several ANDs,
like:



unncessarily? I need to write an IF clause with about 100 of those
comparisons, and I need it to evaluate as quickly as possible. If the answer
to the previous question is yes, what would you suggest I do?

Thanks,
Oz

 
 
 

SQL Server INTERNALS: IF clause

Post by Oz Ben Elieze » Sun, 29 Oct 2000 02:34:20


Sorry for sending another message in the issue, but I would like to enhance
my question.

My IF clause actually looks like this:




    ....



the answer to the previous question is yes, what would you suggest I do to
do those comparisons and evaluations as quickly as possible?

Thanks a lot !
Oz

 
 
 

SQL Server INTERNALS: IF clause

Post by Umachandar Jayachandra » Sun, 29 Oct 2000 14:35:21


    Short-circuiting an expression is not available in T-SQL. The optimizer
can process it any way it wants.
--
Umachandar Jayachandran
SQL Resources at http://www.umachandar.com/resources.htm
( Please reply only to newsgroup. )
 
 
 

SQL Server INTERNALS: IF clause

Post by Keith Kratochvi » Mon, 30 Oct 2000 04:19:07


This same question has been asked within .server
The subject of the post is Re: MS-SQL Server, INTERNALS, IF
clause, -=addition=-
and it is dated 10/27/20001:59pm

I posted this message in that location.
I have also posted it here.

Oz, in the future, please do not post the same question independently to
multiple newsgroups!

Umachandar, Doesn't this example show that SQL Server can short-circuit
expressions?

from my post within .server:
This example should show that SQL Server can short circuit expressions





--This works correctly

print 'test 1'

--This 'short circuits'

print 'test 2'

--This fails

print 'test 3'

--
Keith
==============
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.


Quote:>     Short-circuiting an expression is not available in T-SQL. The
optimizer
> can process it any way it wants.
> --
> Umachandar Jayachandran
> SQL Resources at http://www.umachandar.com/resources.htm
> ( Please reply only to newsgroup. )

 
 
 

SQL Server INTERNALS: IF clause

Post by Umachandar Jayachandra » Mon, 30 Oct 2000 11:28:29


    What I wanted to say was that it will not work the same always. Your
example didn't work for me on SQL70/2000.

--
Umachandar Jayachandran
SQL Resources at http://www.umachandar.com/resources.htm
( Please reply only to newsgroup. )

 
 
 

SQL Server INTERNALS: IF clause

Post by Keith Kratochvi » Mon, 30 Oct 2000 12:36:55


interesting!  It worked on 7.0 SP2.
Maybe it will work sometimes....depending on the situation.....depending on
the alignment of the planets...depending on the day of the week...

--
Keith
==============
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.


Quote:>     What I wanted to say was that it will not work the same always. Your
> example didn't work for me on SQL70/2000.

> --
> Umachandar Jayachandran
> SQL Resources at http://www.umachandar.com/resources.htm
> ( Please reply only to newsgroup. )

 
 
 

SQL Server INTERNALS: IF clause

Post by Kalen Delane » Tue, 31 Oct 2000 03:21:05


SQL Server will do short-circuiting, but the issue is that you can't predict
which condition in the WHERE clause will actually end up being evaluated
first. There is no guarantee that the processing will occur in the order you
wrote them.

HTH
--
Kalen Delaney
MCSE, SQL Server MCT, MVP
www.InsideSQLServer.com
Feed Someone for Free Today:
     www.TheHungerSite.com



> interesting!  It worked on 7.0 SP2.
> Maybe it will work sometimes....depending on the situation.....depending
on
> the alignment of the planets...depending on the day of the week...

> --
> Keith
> ==============
> Please reply only to the newsgroups.
> When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
> can be cut and pasted into Query Analyzer is appreciated.



> >     What I wanted to say was that it will not work the same always. Your
> > example didn't work for me on SQL70/2000.

> > --
> > Umachandar Jayachandran
> > SQL Resources at http://www.umachandar.com/resources.htm
> > ( Please reply only to newsgroup. )

 
 
 

SQL Server INTERNALS: IF clause

Post by Keith Kratochvi » Tue, 31 Oct 2000 23:19:28


Thanks for the clarification!

--
Keith
==============
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.



Quote:> SQL Server will do short-circuiting, but the issue is that you can't
predict
> which condition in the WHERE clause will actually end up being evaluated
> first. There is no guarantee that the processing will occur in the order
you
> wrote them.

> HTH
> --
> Kalen Delaney
> MCSE, SQL Server MCT, MVP
> www.InsideSQLServer.com
> Feed Someone for Free Today:
>      www.TheHungerSite.com