I have a post deployment script in my SQL database project, which registers default values that should be part of the database. I'd want this script to be able to be run multiple times without changing the outcome, so I can run it manually if I need to add just a little modification.
INSERT INTO [UserRole] ([Name])
SELECT 'Regular'
UNION ALL
SELECT 'Admin'
INSERT INTO [UserRight] ([Name])
SELECT 'CanAccessApplicationLogs'
UNION ALL
SELECT 'CanAccessApplicationJobs'
INSERT INTO [UserRoleRight] ([UserRoleId], [UserRightId])
SELECT [Ro].[Id], [Ri].[Id] FROM [UserRight] Ri, [UserRole] Ro
WHERE ([Ro].[Name] = 'Admin' AND
[Ri].[Name] = 'CanAccessApplicationLogs')
OR ([Ro].[Name] = 'Admin' AND
[Ri].[Name] = 'CanAccessApplicationJobs')
I doubt performance will ever be a problem since this script will just be inserting roles and rights. So what would be the cleanliest way to check if each row exists, and only insert those that aren't already in the tables?