Preventing Multiple Inserts with SQL Patch Script -


i have sql script meant work patch, it's intended run 1 time modify entries in database:

insert table1 ... insert table2 ... etc... 

if customer service (or whoever applying patch) run script multiple times accidentally, there duplicate entries made tables have undesirable effect.

my thought have delete statement precede each insert if entry inserted existed, deleted, avoiding duplicate. if entry wasn't there, delete statement have no effect on database.

is there better way handle issue, , if so, why suggested approach considered better or more appropriate? performance isn't concern since script intended run once.

note i'm not asking if way exists. asking, given context (the script patch meant run once, ever), solution described above appropriate or there reason why should avoid going approach , solve issue way?

i use merge statement:

       merge  [dbo].[table] target using (         values                      (100001, 'value1', null, 1)        ,(100002, 'value2', null, 1)                     )          source ([id], [column1], [column2], [column3])             on target.[id] = source.[id]         when matched update set          [column1] = source.[column1]        ,[column2] = source.[column2]        ,[column3] = source.[column3]         when not matched target        insert ([id], [column1], [column2], [column3]) values         (source.[id],source.[column1],source.[column2],source.[column3])        ; 

Comments