在用户完成验证(某种用户键入数据)后,我正在使用捕获软件 Abbyy FlexiCapture。它将捕获的数据导出到 SQL Server 2008 R2
一旦 SQL Server 确定有插入的数据(新行),它将触发代码根据业务规则更新表 Abbyy。
该表有两列称为ProcessingDate
和DateOfService
我的业务规则要求是
- 如果处理日期晚于服务日期 7 个月,它将更新表 Abbyy
CouponStatus
带有“拒绝”的列,RejectCode
带有“ A5 ”的列。
- 如果 Processing Date 早于 Date Of ServiceDate 7 个月,它将更新 Table Abbyy
CouponStatus
带有“批准”的列,RejectCode
带有“null”的列。
我面临一个错误,因为一些处理日期超过服务日期 7 个月的插入数据,列CouponStatus
变为“批准”。
我不确定是我的触发代码问题还是日期格式问题。当用户进行验证时,日期格式为dd/mm/yy
,
在 SQL Server 2008 R2 中,我ProcessingDate
和DateOfService
列的数据类型为date
格式yyyy-mm-dd
这是我的触发代码,请从第 86 行开始
USE [master]
GO
/****** Object: Trigger [dbo].[BusinessRule] Script Date: 10/03/2012 11:28:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [dbo].[BusinessRule]
ON [dbo].[Abbyy]
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @Identity varchar(225);
DECLARE @RegNo varchar(225);
DECLARE @ProDate date;
DECLARE @SerDate date;
DECLARE @PriKey varchar(255);
SELECT @Identity=EngineNo, @RegNo=VehRegNo, @ProDate=ProcessingDate, @SerDate=DateOfService,@PriKey=DocID FROM Inserted
--If EngineNo not exist in db, update Reject & A1
IF EXISTS (Select EngineNo
From Abbyy
Where
NOT EXISTS
(Select EngineNo
From eDaftarOwnerDetail
where eDaftarOwnerDetail.EngineNo = @Identity))
UPDATE Abbyy
SET CouponStatus = 'Reject', RejectCode = 'A1'
WHERE EngineNo = @Identity
and DocID=@PriKey
--If Vehicle Registration No not exist in db, update Reject & A2
Else If EXISTS (Select VehRegNo
From Abbyy
Where
NOT EXISTS
(Select VehRegNo
From eDaftarOwnerDetail
Where eDaftarOwnerDetail.VehRegNo = @RegNo))
UPDATE Abbyy
SET CouponStatus = 'Reject', RejectCode = 'A2'
WHERE VehRegNo = @RegNo
and DocID=@PriKey
--If EngineNo & Vehicle Registration No does not matched, update Reject & A3
Else If EXISTS (Select EngineNo, VehRegNo
From Abbyy
Where
NOT EXISTS
(Select EngineNo, VehRegNo
From eDaftarOwnerDetail
Where eDaftarOwnerDetail.EngineNo = @Identity
and eDaftarOwnerDetail.VehRegNo = @RegNo))
UPDATE Abbyy
SET CouponStatus = 'Reject', RejectCode = 'A3'
WHERE EngineNo = @Identity
and VehRegNo = @RegNo
and DocID=@PriKey
-- If EngineNo exist in db more then twice, update Reject & A4
Else If EXISTS (Select COUNT(1)
From Abbyy
Where EngineNo = @Identity
Group by EngineNo
Having COUNT(1)>2)
UPDATE Abbyy
SET CouponStatus = 'Reject', RejectCode = 'A4'
WHERE EngineNo = @Identity
and DocID=@PriKey
-- If ProcessingDate more than ServiceDate 210 days, update Reject & A5
Else If EXISTS (Select ProcessingDate, DateOfService
From Abbyy
Where
datediff(day, @SerDate, @ProDate)>210)
UPDATE Abbyy
SET CouponStatus = 'Reject', RejectCode = 'A5'
WHERE ProcessingDate = @ProDate
and DateOfService = @SerDate
and DocID=@PriKey
Else
UPDATE Abbyy
Set CouponStatus = 'Approve', RejectCode = ''
WHERE EngineNo = @Identity
-- Insert statements for trigger here
END
感谢任何人都可以给我一些指导来解决这个问题。谢谢你。