T-SQL部署后脚本只运行一次

时间:2018-06-14 21:16:02

标签: sql-server tsql deployment

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

UPDATE Tasks SET duration = duration - 1

如何确保每次发布项目时脚本都不会运行,但只运行一次。有没有办法让SQL Server记住脚本之前已经运行过?

2 个答案:

答案 0 :(得分:1)

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

-- 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

答案 1 :(得分:0)

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

举个例子:

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

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

相关问题