You are here: Home > Technical Articles > MS SQL Server - Delete Record If Exists

MS Sql Server - Delete row from a table if record exists


--SQL Script for deleting a row from a table if the record exists.
IF EXISTS(SELECT groups_guid FROM groups WHERE groups_guid = '11a5ef51-c1cc-46f6-8c3f-d160f4cc06a3')
BEGIN
    DELETE FROM groups WHERE groups_guid = '11a5ef51-c1cc-46f6-8c3f-d160f4cc06a3'
END
GO


-- Insert new record only if it does not already exist.
IF NOT EXISTS (SELECT * FROM priority WHERE ID = 3)
BEGIN
    INSERT INTO priority (ID, priority)
    SELECT 3, 'Normal [Default]'
END
GO


Published On: 02 Aug 2009