Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Quickest way to see if a record exists
Message
 
À
17/09/2008 05:53:43
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Autre
Versions des environnements
SQL Server:
SQL Server 6.5 and older
Divers
Thread ID:
01348134
Message ID:
01348135
Vues:
38
This message has been marked as the solution to the initial question of the thread.
>hi
>what is the best/ quickest way to check if a record exists in a table? thats all I want to know, ie "select * from mytable where myid =lcid " and myId is not a primary key
>thanks
>stuart

There is no need some field to be Primary key to be used in WHERE clause :-).
So if you want to know if there are records where MyId is equal to some value your select is right. But if you want only to know if these records exsits but you don't need to have recordset based on these records you could use EXISTS() function. It stops execution after the first records is found, so that is the quickest way:
IF EXISTS (select * from mytable where myid =lcid)
    BEGIN
         print 'Record(s) exists'
    END
ELSE
    BEGIN
         print 'No record exists'
    END
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform