触发器

Posted 一锤定音

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了触发器相关的知识,希望对你有一定的参考价值。

USE [UFDATA_010_2013]
GO
/****** Object:  Trigger [dbo].[prodb_trigger]    Script Date: 03/12/2016 08:47:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER trigger [dbo].[prodb_trigger] on [dbo].[PRO_Db_b]
for insert,update,delete
as
begin
    declare @inserted int,
            @deleted int,
            @startdate datetime,
            @enddate datetime,
            @depcode nvarchar(50),
            @distributecotton nvarchar(512),
            @dbqty float,
            @dbqtyback float
    select @inserted=COUNT(*) from inserted
    select @deleted=COUNT(*) from deleted
   
    if @inserted>0 and @deleted=0
    begin
        --insert
        select @startdate=startdate,@enddate=enddate,@depcode=depcode,@dbqty=dbqty,@distributecotton=distributecotton from  inserted;
        update a set [email protected] from PRO_Element_b a,PRO_Element b where b.id=a.element_id and b.elementdate>[email protected] and b.elementdate<[email protected] and [email protected] and [email protected]
    end
    else if @inserted>0 and @deleted>0
    begin
        --update
        select @startdate=startdate,@enddate=enddate,@depcode=depcode,@dbqty=dbqty,@distributecotton=distributecotton from  inserted;
        select @dbqtyback=dbqty from  deleted;
        update a set [email protected]@dbqtyback from PRO_Element_b a,PRO_Element b where b.id=a.element_id and b.elementdate>[email protected] and b.elementdate<[email protected] and [email protected] and [email protected]
    end
    else if @inserted=0 and @deleted>0
    begin  
        --delete
        select @startdate=startdate,@enddate=enddate,@depcode=depcode,@dbqty=dbqty,@distributecotton=distributecotton from  deleted;
        update a set [email protected] from PRO_Element_b a,PRO_Element b where b.id=a.element_id and b.elementdate>[email protected] and b.elementdate<[email protected] and [email protected] and [email protected]
    end
end

以上是关于触发器的主要内容,如果未能解决你的问题,请参考以下文章

触发器的应用和用法?

触发器

oracle 触发器

oracle触发器

postgreSQL触发器

oracle中触发器有啥作用