Molitaa Molitaa - 9 months ago 47
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 Source

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

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