Thursday, May 1, 2014

Check to see if a record exists in SQL before making an insert so as not to make a duplicate!

IF NOT EXISTS(SELECT 1 FROM dbo.Foo WHERE Foo_ID=13)
BEGIN
   SET IDENTITY_INSERT dbo.Foo ON
   
   INSERT INTO dbo.Foo (Foo_ID, Name, Description)
   VALUES (13, 'Whatever', 'blah. blah. blah.')
   
   SET IDENTITY_INSERT dbo.Foo OFF
   PRINT('record thirteen was added')
END

No comments:

Post a Comment