Support our 100% FREE Projects: Donate Now OR Sponsor Now


  

sql check if record exists

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

Share: Share on FacebookShare on Google PlusTweet it
Comments Add Comment
No data was returned. Share your thoughts, questions and suggest improvements:
Add Comment

You found us! Below are 50 most popular searched keywords

created at TagCrowd.com

Disclaimer: While every caution has been taken to provide our readers with most accurate information and honest analysis, please use your discretion before taking any decisions based on the information in this blog. Author will not compensate you in any way whatsoever if you ever happen to suffer a loss/inconvenience/damage because of/while making use of information in this blog.