SQL Server 2008 R2中的触发器代码或日期格式错误

时间:2012-10-03 04:15:51

标签: sql triggers sql-server-2008-r2

在用户完成验证(数据中的某种用户密钥)后,我正在使用捕获软件Abbyy FlexiCapture。它会将捕获的数据导出到SQL Server 2008 R2

一旦SQL Server识别出插入数据(新行),它将触发代码更新表Abbyy基于业务规则

该表有两列名为ProcessingDateDateOfService

我的业务规则要求是

  1. 如果处理日期晚于服务日期7个月,则会更新表Abbyy
    • CouponStatus与" 拒绝",
    • RejectCode与" A5 "。
  2. 如果处理日期早于ServiceDate 7月的日期,它将更新表Abbyy
    • CouponStatus与"批准",
    • RejectCode," null"。
  3. 我遇到错误,因为某些处理数据处理日期超过服务日期7个月,列CouponStatus变为" 批准&# 34 ;.

    我不确定是我的触发器代码问题还是日期格式问题。 当用户进行验证时,日期格式为dd/mm/yy

    在SQL Server 2008 R2中,我的ProcessingDateDateOfService列的数据类型为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
    

    感谢任何人都可以给我一些指导来解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:1)

在比较之前进行某种日期格式化,将两个值转换为相同的格式

示例:

WHERE CONVERT(VARCHAR,ProcessingDate,102) = CONVERT(VARCHAR,@ProDate,102)

OR

WHERE CONVERT(DATE,ProcessingDate,102) = CONVERT(DATE,@ProDate,102)