zoukankan      html  css  js  c++  java
  • 触发器与存储过程的应用

    1.当一张表的一条数据发生更新,删除,插入时要改变其他表或者当前表的内容时,我们可以写一个触发器来进行这类操作,下面我举一个最基本的触发器的例子

    create TRIGGER t_bos200000001uodate ON t_bos200000001        --触发器的名字和关联的表
    after UPDATE                                                                              --在更新的时候
    AS
    Declare @Foldlsh int                                                                       --定义相对应的变量
    declare @fbillno varchar(50)
    declare @fbiller int
    Select  @fbillno=fbillno,@fbiller=fbiller from inserted        --要更新的数据        
    Select @Foldlsh = fmulticheckstatus  from deleted          --更新前的数据


    if update(fmulticheckstatus) and @Foldlsh =16                  --触发器的判断
    begin

    exec x_xlh @fbillno,@fbiller                 --执行的逻辑

    end

    2.这里我们会看到exec x_xlh @fbillno,@fbiller,x_xlh就是对应的存储过程,@fbillno,@fbiller就是传进去的参数

    因为要做的逻辑比较多,所以我们把主要的逻辑放到存储过程里面,下面我们就是存储过程详细的代码列出来。

    create PROCEDURE x_xlh
      @fbillno varchar(50),@fbiller int                   --这两个就是要传进去的参数
    AS
    BEGIN   

    --定义变量与取值的过程           
    declare @FInterid2 int
    declare @FInterid int
    declare @fcustid int
    DECLARE @fdcstockid int
    declare @fbase2 varchar(50)
    declare @fbaseproperty varchar(50)
    declare @fbaseproperty2 varchar(50)
    declare @finteger int
    declare @fentryid int
    declare @fitemid int
    declare @fid int
    SET NOCOUNT ON;
    select 1
    select top 1 @fcustid=a1.fbase5 from t_bos200000001entry2 a1 inner join t_bos200000001 b1 on a1.fid=b1.fid where b1.fbillno=@fbillno
    select @fbiller
    select @fdcstockid=535
    exec GetICMaxNum 'ICStockbill',@FInterid2 output ,29,16394
    select @fentryid=1

    insert into ICStockBill (FBrNo,FInterID,FTranType,FDate,FBillNo,FNote,
    FSupplyID,FFManagerID,FSManagerID,FBillerID,
    FROB,FUpStockWhenSave,FOperDate,FMarketingStyle,
    FSelTranType,FsourceType,
    FPurposeID,FCussentAcctID,FPayCondition
    ,FSettleDate,FDCStockID )
    values ('0',@FInterid2,'29',convert(varchar(10),getdate(),120),@fbillno,'',
    @fcustid,'','',@fbiller,
    '1','1',null,'12530',
    '','37521'
    ,14190,1104,1000
    ,convert(varchar(10),getdate(),120),@fdcstockid )
    END
    GO

    3.这样我们就完成了用存储过程插入一张表的逻辑,而且可以用触发器触发这个存储过程

  • 相关阅读:
    理解 RESTful:理论与最佳实践
    Shiro 性能优化:解决 Session 频繁读写问题
    单点登录的三种实现方式
    理解 Spring(二):AOP 的概念与实现原理
    理解 Spring(一):Spring 与 IoC
    MFC查内存泄漏方法
    024 --- 第28章 访问者模式
    023 --- 第27章 解释器模式
    022 --- 第26章 享元模式
    021 --- 第25章 中介者模式
  • 原文地址:https://www.cnblogs.com/xujiating/p/6247510.html
Copyright © 2011-2022 走看看