Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Fail fast with AND?

Author  Topic 

Samadhi69
Starting Member

20 Posts

Posted - 2009-06-25 : 12:59:05
With imperative languages you can avoid some steps by failing fast with an AND declaration. IE if you have two conditions and the second may give you some error, but can only occur if the first condition is false then you write the code something like
if (condition1 AND condition2) {stuff}
Because if condition1 is false it stops there.

I'm trying to use this on a query to apply flag values in a table. The logic is based on whether any of 7 fields is over a value. Unfortunately the field is varchar and there are text values on some. However, these rows can be ignored when they have any text entries. So imperatively a failfast AND of checking for text then checking the 7 or statements would work.

With SQL (I'm guessing because it's set based) it will not do this.

The rows do not have unique keys (don't blame me I didn't create the db) so I can't use a where in (select ...) or something similar.

Does anyone have suggestions?

If con is the opposite of pro, what's the opposite of progress?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-25 : 13:36:20
you can add a new condition to this expression like
(ISNUMERIC(yourflagfield)=0 OR (ISNUMERIC(yourflagfield)=1 AND CHARINDEX('d',yourflagfield)>0 AND CHARINDEX('e',yourflagfield)>0)) OR (condition1 AND condition2))

so that other conditions will be evaluated only for numeric values
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2009-06-25 : 14:28:48
I don't think you can guarantee the order in which SQL will evaluate the expressions. Can you?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-25 : 14:30:07
I don't think so.
SQL Server treats all WHERE in a cost-based manner.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

Samadhi69
Starting Member

20 Posts

Posted - 2009-06-25 : 16:49:43
I think for the purposes of this I'm going to set all the text values to '-1'. Fortunately I don't need to save them for this report otherwise I think I'd have to use a case statement which would slaughter my performance.

If con is the opposite of pro, what's the opposite of progress?
Go to Top of Page
   

- Advertisement -