Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Check if PK value is in table
Message
De
09/12/2005 09:30:07
Keith Payne
Technical Marketing Solutions
Floride, États-Unis
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Stored procedures, Triggers, UDFs
Versions des environnements
SQL Server:
SQL Server 2000
Divers
Thread ID:
01076415
Message ID:
01076579
Vues:
15
>>>Is this (see code snippet below) the most efficient way to check if there is a row in the table with a given PK value?
>>>
>>>
>>>IF ( SELECT COUNT(*) FROM MyTable WHERE PkId = @PkId ) < 1
>>>BEGIN
>>>	RAISERROR('The record you are looking for aint there.',16,1)
>>>END
>>>
>>>
>>>Einar
>>
>>
IF NOT EXISTS(SELECT [PK] FROM [MyTable] WHERE [PK] = @PK) ...
>>
>>If this query is used a lot, there should be a nonclustered index on PK.
>
>Thanks. Your way looks cleaner than my code.
>
>Einar

Use IF EXISTS instead of COUNT() whenever possible. The performance benefits can be extreme when there is no index for the WHERE clause. And when there is an index, there is still a performance benefit.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform