首先,我不是一位经验丰富的程序员,所以这个问题可能看起来有些愚蠢。我编写了一个触发器,用于在第一个表中的状态字段更新时向第二个表插入一行。问题是,它插入相同的行两次,第二个问题是如果我更新第一个表中的任何字段,此触发器运行。请帮忙。这就是触发器的样子
CREATE TRIGGER [c_CRM].[TR_TICKETLOG]
ON [c_CRM].[T_SUPPORT_TICKETS]
AFTER INSERT, UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @TicketID int = (SELECT F_Ticket_ID FROM INSERTED)
DECLARE @StatusID int = (SELECT F_Ticket_Status_ID FROM INSERTED)
DECLARE @UpdateID int = (SELECT F_Ticket_Updated_By FROM INSERTED)
DECLARE @UserID int = (SELECT userID FROM FLEXVIEWS_USERS WHERE userID=@UpdateID)
IF(@StatusID IS NOT NULL)
BEGIN
DECLARE @message nvarchar(max) = (CASE @StatusID WHEN '1' THEN 'Ticket status set to Ticket Created' WHEN '2' THEN 'Ticket status set to Service in progress' WHEN '3' THEN 'Ticket status set to Awaiting client response' WHEN '4' THEN 'Ticket status set to Closed' WHEN '5' THEN 'Ticket status set to Pending' WHEN '6' THEN 'Ticket status set to Ticket Removed' ELSE ' ' END);
INSERT INTO c_CRM.T_TICKET_LOG (F_Ticket_Log_Ticket_ID, F_Ticket_Log_User_ID, F_Ticket_Log_Time, F_Ticket_Log_Message) VALUES (@TicketID, @UserID, GETDATE(), @message);
UPDATE T_SUPPORT_TICKETS SET F_Ticket_Updated_On = GETDATE() WHERE F_Ticket_ID = @TicketID;
END
ELSE
UPDATE c_CRM.T_TICKET_LOG
SET F_Ticket_Log_Message = NULL
WHERE F_Ticket_Log_Ticket_ID = @TicketID;
END