如果 table 中的字段减少(序列号控制 Table),则在 SQL 中创建警报

Create an Alert in SQL if a field in a table decreases (Seq number Control Table)

我有一个 table 用于存储工作和发票的序列号。 本周早些时候,其中一个序列号从 100000 奇数重置为 1。

我想创建某种警报,如果序列号随时减少,它会通知我,以便我可以当场主动解决问题。

Table 看起来像这样:

Comp_id - 这将始终相同,例如。比赛

Seq_type - 序列名称,即发票

u_version - 始终为“#”

Seq_no - 创建新发票时递增的序号。

Seq_desc - seq_type

的定义

SP_str1 - 空值

SP_num1 - 空值

当 SEQ_TYPE 随时降低其值时,我需要警报通知我。

我愿意接受任何建议。

我会使用触发器触发警报:

Create trigger trg_u_Sequence on [Sequence]
for update
as 
begin
select [existing].Seq_type as OLDSeq_type, [existing].Seq_no as OLDSeq_no, [new].Seq_type as NEWSeq_type, [new].Seq_no as NEWSeq_no
into #temp
from inserted [new]
inner join deleted [existing]
    on [new].seq_type = [existing].seq_type
where [new].Seq_no <= [existing].seq_type
if @@ROWCOUNT > 0
    begin
    --Alert using sp_send_dbmail or write to a table with the contents of #temp
    end
end