Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Logical short circuits in queries?
Message
 
 
To
09/08/2004 20:16:44
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00931921
Message ID:
00931922
Views:
24
Hi David,

T-SQL doesn't have logical data type. You'll have to use numeric or character.
DECLARE @found int
SET @found = CASE 
	WHEN EXISTS  (SELECT * FROM table1  WHERE field1 = myValue) THEN 1
	WHEN EXISTS  (SELECT * FROM table2  WHERE field2 = myValue) THEN 1
	...
	ELSE 0 END
Take a look also at Re: Can stored procedures return values? Message #876903

>I am not experienced with SQL, but I have a basic understanding.
>
>I want to determine if a value exists in any of a set of tables. I want to stop after I find the first instance. The pseudocode for a simplified case would be,
>
IF EXISTS
>  (SELECT TOP 1 field1
>   FROM table1
>   WHERE field1 = myValue)
>THEN
>   RETURN true
>ELSE IF EXISTS
>  (SELECT TOP 1 field2
>   FROM table2
>   WHERE field2 = myValue)
>THEN
>  RETURN true
>ELSE
>  RETURN false
>END IF
I eventually need this to be in a stored procedure. I have to believe that only querying the minimum necessary tables is more efficient than unioning a couple the separate queries.
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform