The CREATE TABLE [HistoryItems] (
[ID]/int IDENTITY (1, 1) NOT NULL,
[HTID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[TrayNum] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[StuffID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[StuffName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[SiloID] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[SiloName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[TargetNum] [numeric] (12, 2) NULL,
[FormulaNum] [numeric] (12, 2) NULL,
[StuffUsedNum] [numeric] (12, 2) NULL,
[QueryNum] [numeric] (12, 2) NULL,
[FallNum] [numeric] (12, 2) NULL,
[WaterRate] [numeric] (12, 2) NULL,
[SandRate] [numeric] (12, 2) NULL,
[CheckNum] [ntext] COLLATE Chinese_PRC_CI_AS NULL,
[FormulaOneNum] [numeric] (12, 2) NULL,
[StuffMark] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[Fixed] [int] NULL,
PRIMARY KEY CONSTRAINT [PK_HistoryItems] NONCLUSTERED
(
[ID]
) ON the (PRIMARY)
) ON the [PRIMARY] TEXTIMAGE_ON (PRIMARY)
GO
The structure of the above, is in table 1 and table 2,
Two same, structure,
INSERT INTO [ZL_NCMS] [dbo]. [HistoryItems] ([ID], [HTID], [TrayNum], [StuffID], [StuffName], [SiloID], [SiloName], [TargetNum], [FormulaNum], [StuffUsedNum], [QueryNum], [FallNum], [WaterRate], [SandRate], [CheckNum], [FormulaOneNum], [StuffMark], [Fixed])
VALUES (& lt; ID, int, & gt; , & lt; HTID, varchar (50), & gt; , & lt; TrayNum, varchar (50), & gt; , & lt; StuffID, varchar (50), & gt; , & lt; StuffName, varchar (50), & gt; , & lt; SiloID, varchar (50), & gt; , & lt; SiloName, varchar (50), & gt; , & lt; TargetNum, numeric (12, 2), & gt; , & lt; FormulaNum, numeric (12, 2), & gt; , & lt; StuffUsedNum, numeric (12, 2), & gt; , & lt; QueryNum, numeric (12, 2), & gt; , & lt; FallNum, numeric (12, 2), & gt; , & lt; WaterRate, numeric (12, 2), & gt; , & lt; SandRate, numeric (12, 2), & gt; , & lt; CheckNum ntext, & gt; , & lt; FormulaOneNum, numeric (12, 2), & gt; , & lt; StuffMark, varchar (50), & gt; , & lt; Fixed, int, & gt;)
The above is to insert new records,
- create history insert trigger HistoryItems_Insert
CREATE the TRIGGER HistoryItems_Insert ON dbo. HistoryItems
- just insert new record
AFTER INSERT
AS
The BEGIN
SET NOCOUNT ON
- statement field variablesDeclare @ ID int, @ QueryNum2 numeric (3, 2), @ TargetNum3 numeric (3, 2), @ StuffUsedNumnew numeric (9, 2), @ Stuffid2 int
Declare @ HTID varchar (50), @ TrayNum varchar (50), @ StuffID varchar (50), @ StuffName varchar (50), @ SiloID varchar (50), @ SiloName varchar (50)
Declare @ TargetNum numeric (12, 13), @ FormulaNum numeric (12, 13), @ StuffUsedNum numeric (12, 13), @ QueryNum numeric (12, 13), @ FallNum numeric (12, 13), @ WaterRate numeric (12, 13), @ SandRate numeric (12, 13), @ CheckNum varchar (50), @ FormulaOneNum numeric (12, 13), @ StuffMark varchar (50), @ Fixed int
- take inserted inside the value assigned to the variable
Select @ ID=ID, @ HTID=HTID, @ TrayNum=TrayNum, @ StuffID=StuffID, @ StuffName=StuffName, @ SiloID=SiloID, @ SiloName=SiloName, @ TargetNum=TargetNum, @ FormulaNum=FormulaNum, @ StuffUsedNum=StuffUsedNum, @ QueryNum=QueryNum, @ FallNum=FallNum, @ WaterRate=WaterRate, @ SandRate=SandRate, @ CheckNum=', @ FormulaOneNum=FormulaOneNum, @ StuffMark=StuffMark from INSERTed;
- the original real record is inserted into the new table,
Insert Into dbo. HistoryItems2 (HTID TrayNum, StuffID, StuffName, SiloID, SiloName, TargetNum, FormulaNum, StuffUsedNum, QueryNum, FallNum, WaterRate, SandRate, CheckNum, FormulaOneNum, StuffMark) values (@ HTID @ TrayNum, @ StuffID, @ StuffName, @ SiloID, @ SiloName, @ TargetNum, @ FormulaNum, @ StuffUsedNum, @ QueryNum, @ FallNum, @ WaterRate, @ SandRate, @ CheckNum, @ FormulaOneNum, @ StuffMark);
- take material coding id judgment is a material or a,
The set @ Stuffid2=CAST (@ StuffID as int);
- determine target is greater than zero, hit the condition change
If (@ TargetNum> 0)
The begin
The set @ QueryNum2=(@ StuffUsedNum - @ TargetNum)/(@ TargetNum/100); - their calculation error rate
- determine material as the aggregate and the error rate is greater than 2
If (@ Stuffid2 & lt;=6 and @ QueryNum2 & gt; 2)
- update the actual using value StuffUsedNum field is in 0 a small digital use fixed error rate target + * 1.5/100. 1.5 error rate target in HistoryItems,
The update HistoryItems set StuffUsedNum=round (@ TargetNum + @ TargetNum * 1.5/100, 0) from inserted - where HTID=@ HTID;
- determine material as the aggregate and the error rate is greater than 2
Else if (@ Stuffid2 & lt;=6 and @ QueryNum2 & lt; - 2)
- update the actual using value StuffUsedNum field is in 0 a small digital use fixed error rate target + * - 1.5/100. 1.5 error rate target in HistoryItems,
The update HistoryItems set StuffUsedNum=round (@ TargetNum + @ TargetNum * - 1.5/100, 0) from inserted - where HTID=@ HTID;
- determine material for powder water, admixture and error rate is greater than 2
Else if (@ Stuffid2 & gt;=7 and @ QueryNum2 & gt; 2)
- update the actual using value StuffUsedNum field is in one small digital use fixed error rate target + * 1/100. 1.5 error rate target in HistoryItems,
The update HistoryItems set StuffUsedNum=round (@ TargetNum + @ TargetNum * 1/100, 1) the from inserted - where HTID=@ HTID;
- determine material for bone water, additive and error rate is greater than 2
Else if (@ Stuffid2 & gt;=7 and @ QueryNum2 & lt; - 2)
- update the actual using value StuffUsedNum field is in one small digital use fixed error rate target + * - 1/100. 1.5 error rate target in HistoryItems,
nullnullnullnullnullnullnull