Search This Blog

Friday, November 12, 2010

Checking data in database before going for any inserts

We commonly encounter a scenario where we have to check if a data is already there in table before going for any insertions. So here is a quick query for this purpose

please note that i wrote this query keeping Sql Server in mind.

IF NOT EXISTS ( SELECT TOP 1 [NAME] FROM myTable WHERE [Name] = 'check_name' )
BEGIN

INSERT INTO myTable
           ([Name]
           ,[Description]
           )
     VALUES
           ('check_name'
           ,'test'
           )
END

No comments:

Post a Comment