代码之家  ›  专栏  ›  技术社区  ›  koryakinp

t-sql部署后脚本只运行一次

  •  0
  • koryakinp  · 技术社区  · 7 年前

    我有 Tasks A表 duration 列。我想创建一个部署后脚本,将该列的值递减1。

    UPDATE Tasks SET duration = duration - 1
    

    如何确保脚本不会在每次发布项目时运行,而是只运行一次。有没有办法让sql server记住脚本以前已经运行过?

    2 回复  |  直到 7 年前
        1
  •  1
  •   jspcal    7 年前

    部署后脚本设计为在每次部署项目时执行。脚本应该是可重复的。您可以在脚本中实现自己的保护逻辑,以确保它只在适当时运行。在这种情况下,您可以在一个单独的状态表中检查标志,该表跟踪您是否已经执行了更新。例如:

    -- Create a state table with a single row
    
    create table deployment_script_state (duration_was_updated int);
    insert into deployment_script_state values (0);
    
    -- Perform the operation if the update hasn't been done
    
    update t
    set t.duration = t.duration - 1
    from Tasks t
    join deployment_script_state d
    on d.duration_was_updated = 0
    
    -- Set update flag to done
    
    update deployment_script_state
    set duration_was_updated = 1
    
        2
  •  0
  •   SQLApostle    7 年前

    我的实现方法是使用一个项目变量

    例如:

    IF '$(DeployJobs)' = 'true'
    BEGIN
        PRINT 'The Jobs will be deployed as the DeployJobs flag has been set'
        ......
    END
    

    这允许我使用部署变量控制脚本是否运行