操作数类型冲突:日期与PRINT语句中的int与变量不兼容

时间:2017-11-21 21:18:29

标签: sql-server

我正在尝试在MS sql server中创建一个触发器,但我一直收到'操作数类型冲突:日期与int不兼容'错误。我认为错误来自我的PRINT语句,但我对sql不够熟悉,知道如何克服这个问题。

ALTER TRIGGER utrInsteadUpdateTblMembership
ON tblMembership INSTEAD OF UPDATE
AS
BEGIN
    DECLARE @newPayDate date, @prevPayDate Date, @prevMemberNumber int

    Select @newPayDate = MembershipFeePaidDate FROM inserted
    Select @prevPayDate = MembershipFeePaidDate FROM deleted
    Select @prevMemberNumber = MembershipID FROM deleted 


    IF @prevPayDate IS NOT NULL AND @prevPayDate > @newPayDate
        BEGIN 
            UPDATE tblMembership 
                SET MembershipFeePaidDate = @newPayDate 
                WHERE @prevMemberNumber = MembershipID 
            UPDATE tblMembership 
                SET isCurrentMember = 1
                WHERE @prevMemberNumber = MembershipID 
            PRINT 'Membership Number ' + @prevMemberNumber + ':' + ' payment date is ' + @newPayDate + ';' + ' prior payment date is ' + @prevPayDate
        END
    ELSE
        BEGIN
            PRINT 'Membership Number ' + @prevMemberNumber + ':' + ' new payment date ' + @newPayDate + ' is earlier than prior payment date ' + @newPayDate +';' + 'No change made' 
        END
END

1 个答案:

答案 0 :(得分:2)

这是因为它将其解释为加法与连接。这同样适用于Dynamic SQL

 PRINT 'Membership Number ' + cast(@prevMemberNumber as char(8)) + ':' + ' new payment date ' + convert(varchar(10),@newPayDate,101) + ' is earlier than prior payment date ' + convert(varchar(10),@newPayDate,101) +';' + 'No change made'