Making Deployments Simpler with Re-runnable Scripts

  • For DML kind of statements an approach i follow is to insert records which do not already exist in the target table. Rather than hard coding which can create unique constraint violations or primary key violations

    Eg:
    say i have a target table

    t_output(x int identity primary key
            ,y varchar(100)
            )

    that has the following values

    t_output
    ---------
    x y
    1 'USA'
    2 'UK'
    3 'Singapore'

    If I need to load 'China' and 'India' to this list.

    Then i would do as follows
    create table #t(x int, y varchar(100))

    insert into #t(4,'China')
    insert into #t(5,'India')

    insert into t_output(y)
    select a.y
      from #t a
    where not exists(select 1
                       from t_output b
                      where b.y=a.y) /*join based on the natural key of the t_output table*/


    Then if i run this code in any of the environments such as uat/Pre-Prod/Prod and if there already exists entries for China/India created by someone else, then i wouldnt be faced with a logical corrupt table.

Viewing post 16 (of 15 total)

You must be logged in to reply to this topic. Login to reply