触发器与存储过程的应用

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

posted @ 2017-01-04 10:00  许佳挺  阅读(1079)  评论(0编辑  收藏  举报