SQL SERVER错误中的OUTPUT子句

时间:2016-04-08 12:56:19

标签: sql-server

我创建了一个触发器,以便在社交媒体网站中添加两个朋友之后输出此触发器。我正在尝试输出一个临时触发器,以便在输出视图中他们很快就会显示一条语句。请问以下代码中输出的语法错误是什么?如果代码有可能改进,请告知我。

CREATE TRIGGER [a01].[trg_addedFriend]
ON [a01].[tbl_friends]
AFTER INSERT

AS
    BEGIN

    DECLARE @FriendA NVARCHAR(45)
    DECLARE @FriendB NVARCHAR(45)

    SELECT @FriendA = ownerAccountID
        FROM inserted;
    SELECT @FriendB = friendAccountID 
        FROM inserted;

    OUTPUT 'Trigger : '+@FriendA+' and '+@FriendB+' become friends.'
    END
GO

1 个答案:

答案 0 :(得分:1)

  1. 我想你想要PRINT声明:

    创建触发器[a01]。[trg_addedFriend]       ON [a01]。[tbl_friends]     插入后

    AS
        BEGIN
    
        DECLARE @FriendA NVARCHAR(45)
        DECLARE @FriendB NVARCHAR(45)
    
        SELECT @FriendA = ownerAccountID
            FROM inserted;
        SELECT @FriendB = friendAccountID 
            FROM inserted;
    
        Print 'Trigger : '+@FriendA+' and '+@FriendB+' become friends.'
        END
    GO
    
  2. 触发器基于设置。不要逐行申请""触发器中的逻辑。

  3. http://www.jimmcleod.net/blog/index.php/2008/06/05/triggers-set-based-not-row-based/