Molitaa Molitaa - 1 day ago 4
SQL Question

How to check if there is a record in a table?

I want to make a query where I can go to a table and ask if there are records with the selected id.

If there are records that show me and if not send me a message where I indicate there are no records to show.

Someone can guide me a little.
Thank you.

Answer

To check for existence of records you can use this code:

IF EXISTS(SELECT TOP(1) 1 FROM schema.table WHERE condition)
BEGIN
 SELECT field FROM schema.table WHERE condition;
END
ELSE
BEGIN
 --do something for no records
END
Comments