Molitaa Molitaa - 4 days ago 5
SQL Question

Query to check if there is a record of a certain ID in a table

I want to make a query to check if there are records with the selected id.

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