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.这样我们就完成了用存储过程插入一张表的逻辑,而且可以用触发器触发这个存储过程

  • 相关阅读:
    iReaper
    展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告(turn)
    用C#写ExtJS代码的开源工具extsharp
    如何你是公司的HR,去招聘asp.net程序员,你会对前来面试的人问什么问题。
    ExtJS 3.0 Designer Preview (官方的IDE可视化工具)
    Asp.net ajax、Anthem.net、Ajax pro三大ajax框架那一种使用比较方便?易于配置?
    C#和ASP.net程序员招聘技能要求
    序列化上面创建的Person对象,使其成为一个JSON字符串
    10大加速Ajax开发的框架
    android 解决wifi断线不稳定的问题终极办法
  • 原文地址:https://www.cnblogs.com/xujiating/p/6247510.html
Copyright © 2011-2022 走看看