In this SQL Tutorial I will show you how to using SQL check if record exists the sample below is created using SQL Server 2008 R2 but should work in most SQL Server versions.
In order to check if a row exists in a table I will use IF with EXISTS function and query that returns only rows with static value 1
Below is an example to check if DimDate table contains record with ID = -1
if exists(select 1
from dbo.DimDate
where ID = -1 )
print 'exists'
As you can see it is a simple matter of writing query and using IF EXISTS to check if a record exists or not
So what will happen if my query criteria returns more than one row? Not much the end result is still the same. See below
if exists(select 1
from dbo.DimDate
where ID > 0 )
print 'exists'
More examples are links below
SQL check if record exists before insert
SQL Check if record exists before update
I hope the above example will help you to create sql that checks if record exists
Take care
Emil