sql-server - 带有等待子句的存储过程
问题描述
我有以下 SQL 服务器stored procedure
,我的第一个存储过程,需要一些帮助。
是否可以添加某种类型的“WAIT_TRIGGER”?我需要检查一个表,如果表中存在该值,则继续执行 sp。如果不是,则添加 5 分钟等待,然后重试 exec,继续循环此过程,直到值存在。
我不知道我所问的是否需要更多时间来实施,或者这是否可能。
if exists
(select RUN_DT
from PROCESS_TBL
where 1=1 and RUN_DT = cast(getdate() as date) )
--else return to top and wait 5 minutes?
所有代码:
USE [MAIN]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[sp_MAIN_insert]
@packagename as varchar(255)
as
begin
declare @returnhome as varchar(1)
-- set nocount on added to prevent extra result sets from
-- interfering with select statements.
set nocount on;
if @packagename='MAIN_insert'
begin
if exists
(select RUN_DT
from PROCESS_TBL
where 1=1 and RUN_DT = cast(getdate() as date) )
begin
declare
@curr_dt date = cast(getdate() as date),
@prev_dt date = cast(getdate()-1 as date)
;
insert into CHK_DATA
(run_dt,
db,
[schema],
[table],
comment
)
-------------------
select
cast(getdate() as date)run_dt,
'MAIN' as 'db',
'dbo' as [schema],
'CHK' as [table],
'test' as comment
from CHK_DATA;
end
end
end
GO
解决方案
你可以试试
while 1=1 or (other exit condition)
begin
if exists(...)
begin
end
else
waitfor delay '0:05:00'
end
等待 5 分钟,或任何低至 ms 的时间段
推荐阅读
- postgresql - PostgreSQL 同步复制一致性
- python - 安装 polyglot 时出现 Unicode 解码错误
- javascript - JSdoc:函数参数中的节点元素类型
- c# - SQL Query 在生产环境中执行时间过长
- ios - 如何在swift中集成Dialogue flow V2?
- python - 从 image_dataset_from_directory 函数生成的数据集不包括批量大小
- html - 在 SCSS 中处理伪类和 BEM 修饰符
- javascript - 从字符串中删除所有出现的“^”
- asp.net - 如何在 ASP .Net 中创建多个 CSV 文件
- node.js - 如何捆绑主干应用程序