I am using a capturing software, Abbyy FlexiCapture, after user done the verification (some sort of user key in data). It will export captured data into SQL Server 2008 R2
Once SQL Server identify there is an inserted data (new row), it will trigger code to update table Abbyy base on business rule.
The table has two columns called ProcessingDate and DateOfService
My business rule requirement is
- If Processing Date is later than Date Of Service 7 month, it will update Table Abbyy
- column
CouponStatuswith “Reject“, - column
RejectCodewith “A5“.
- column
- If Processing Date is earlier than Date Of ServiceDate 7 month, it will update Table Abbyy
- column
CouponStatuswith “Approve”, - column
RejectCodewith “null”.
- column
I am facing an error because some inserted data with Processing Date more than Service Date 7 month, column CouponStatus become “Approve“.
I am not sure is my trigger code problem or date format problem.
when user do validation, the date format is dd/mm/yy,
In SQL Server 2008 R2, my ProcessingDate and DateOfService column’s datatype is date with format yyyy-mm-dd
Here are my trigger code, pls focus start on line 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
Appreciate anyone can give me some guideline to solve this problem. Thank you.
Do some kind of date formatting before comparison to convert both values to the same format
Example :
OR