CASE语句中的嵌套PRINT

时间:2014-03-23 19:35:28

标签: sql-server printing triggers case

CREATE TRIGGER [dbo].[tr_Persons_Update_CASE]
   ON [dbo].[Persons]
   AFTER UPDATE
AS BEGIN
    SET NOCOUNT ON;

    UPDATE Persons SET
        FirstName    = (CASE WHEN D.FirstName <> I.FirstName THEN I.FirstName END),
        LastName = (CASE WHEN D.LastName <> I.LastName THEN I.LastName END),        
        ID_Titlu = (CASE WHEN D.ID_Titlu <> I.ID_Titlu THEN I.ID_Titlu END)             

    FROM DELETED AS D
        INNER JOIN INSERTED AS I ON D.Person_ID=I.Person_ID


END

我正在尝试创建一个仅更新已修改列的触发器。我想为每个CASE分支打印一个语句,以便观察哪一个已被执行。任何人都可以帮助我吗?

我也试过这个,但我在第10,17,26,34行遇到了多个错误Msg 156, Level 15, State 1, Procedure tr_Persoane_Update_Elegant, Line 10

ALTER TRIGGER [dbo].[tr_Persons_Update_Elegant]
   ON [dbo].[Persons]
   AFTER UPDATE
AS BEGIN
    SET NOCOUNT ON;

    SELECT  D.FirstName, D.LastName, D.ID_Titlu, I.FirstName, I.LastName, I.ID_Titlu,
            CASE WHEN D.FirstName <> I.FirstName
                    THEN
                        BEGIN
                            UPDATE Persons
                            SET  FirstName = I.FirstName
                            WHERE FirstName = D.FirstName
                            PRINT 'FirstName  has been changed'
                        END
            /*END                       
            CASE*/ WHEN D.LastName <> I.LastName
                    THEN
                        BEGIN
                            UPDATE Persons
                            SET  LastName = I.LastName
                            WHERE LastName = D.LastName
                            PRINT 'LastName has been changed'
                        END 
            /*END                       
            CASE*/ WHEN D.Title_ID <>   I.Title_ID
                    THEN
                        BEGIN
                            UPDATE Persons
                            SET Title_ID=I.Title_ID
                            WHERE Title_ID=D.Title_ID
                        END
            END
    FROM DELETED AS D
        INNER JOIN INSERTED AS I ON D.ID_Person=I.ID_Person                     

END

0 个答案:

没有答案