ALTER TRIGGER [AddDataToTempTable] ON [dbo].[SC_FHD_T]
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
/*将数据根据需求放入指定表中 2007-08-01 10:20*/
ALTER TRIGGER [AddDataToTempTable] ON [dbo].[SC_FHD_T]
FOR INSERT, UPDATE
AS
if exists(SELECT 1 FROM sysobjects WHERE name='Temp_SendBill')
begin
declare @ID varchar(40)
declare @FHDBH varchar(40)
declare @RWDBH varchar(40)
declare @SGDW varchar(98)
declare @GCMC varchar(98)
declare @JBCCH varchar(40)
declare @JSRYXM varchar(40)
declare @XSFL varchar(10)
declare @ZZJG varchar(98)
declare @RQSJ datetime
declare @CZSJ varchar(8)
declare @SGBW varchar(98)
declare @TLD varchar(40)
declare @QDDJ varchar(98)
declare @HTBH varchar(40)
declare @DJC varchar(10)
declare @LJXSFL varchar(10)
declare @YJ varchar(4)
declare @ZT varchar(8)
declare @isAddData bit
set @isAddData=0
Select
@ID=[ID],
@FHDBH=FHDBH,
@RWDBH=RWDBH,
@SGDW=SGDW,
@GCMC=GCMC,
@JBCCH=JBCCH,
@JSRYXM=JSRYXM,
@XSFL=XSFL,
@ZZJG=ZZJG,
@RQSJ=RQSJ,
@CZSJ=CZSJ,
@SGBW=SGBW,
@TLD=TLD,
@QDDJ=QDDJ,
@HTBH=HTBH,
@DJC=DJC,
@LJXSFL=LJXSFL,
@YJ=YJ,
@ZT=ZT
From inserted
SET Nocount ON
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
Begin Transaction
if (Isnull(@JBCCH,'')<>'')
begin
if (Isnull(@ZT,'')<>'作废')
begin
if exists(select JBCCH from Temp_SendBill where JBCCH=@JBCCH)
begin
declare @tempID varchar(40)
select @tempID=[ID] from Temp_SendBill where JBCCH=@JBCCH
if (@tempID>=@ID)
begin
delete from Temp_SendBill where JBCCH=@JBCCH
set @isAddData=1
end else set @isAddData=0
end
else
begin
declare @hour_dif int
set @hour_dif=DATEDIFF(Hour,@RQSJ,getdate())
if @hour_dif<=2
begin
set @isAddData=1
end
else set @isAddData=0
end
if @isAddData=1
begin
Insert into Temp_SendBill
(
[ID],
FHDBH,
RWDBH,
SGDW,
GCMC,
JBCCH,
JSRYXM,
XSFL,
ZZJG,
RQSJ,
CZSJ,
SGBW,
TLD,
QDDJ,
HTBH,
DJC,
LJXSFL,
YJ,
ZT
)
values
(
@ID,
@FHDBH,
@RWDBH,
@SGDW,
@GCMC,
@JBCCH,
@JSRYXM,
@XSFL,
@ZZJG,
@RQSJ,
@CZSJ,
@SGBW,
@TLD,
@QDDJ,
@HTBH,
@DJC,
@LJXSFL,
@YJ,
@ZT
)
end
end
else
begin
delete from Temp_SendBill where [ID]=@ID
end
end
if @@error<>0
begin
Rollback transaction
end else Commit
end
--------------SET Nocount ON
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
Begin Transaction
if (Isnull(@JBCCH,'')<>'')
begin
if (Isnull(@ZT,'')<>'作废')
begin
if exists(select JBCCH from Temp_SendBill where JBCCH=@JBCCH)
begin
declare @tempID varchar(40)
select @tempID=[ID] from Temp_SendBill where JBCCH=@JBCCH
if (@tempID>=@ID)
begin
delete from Temp_SendBill where JBCCH=@JBCCH
set @isAddData=1
end else set @isAddData=0
end
else
begin
declare @hour_dif int
set @hour_dif=DATEDIFF(Hour,@RQSJ,getdate())
if @hour_dif<=2
begin
set @isAddData=1
end
else set @isAddData=0
end
if @isAddData=1
begin
Insert into Temp_SendBill
(
[ID],
FHDBH,
RWDBH,
SGDW,
GCMC,
JBCCH,
JSRYXM,
XSFL,
ZZJG,
RQSJ,
CZSJ,
SGBW,
TLD,
QDDJ,
HTBH,
DJC,
LJXSFL,
YJ,
ZT
)
values
(
@ID,
@FHDBH,
@RWDBH,
@SGDW,
@GCMC,
@JBCCH,
@JSRYXM,
@XSFL,
@ZZJG,
@RQSJ,
@CZSJ,
@SGBW,
@TLD,
@QDDJ,
@HTBH,
@DJC,
@LJXSFL,
@YJ,
@ZT
)
end
end
else
begin
delete from Temp_SendBill where [ID]=@ID
end
end
if @@error<>0
begin
Rollback transaction
end else Commit
end
主要是这一段不理解