Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Logical short circuits in queries?
Message
 
 
À
09/08/2004 20:16:44
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
00931921
Message ID:
00931922
Vues:
23
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--
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform