BakingCake
BakingCake

Reputation: 353

OUTPUT Clause in SQL SERVER error

I created a trigger so that after two friends are added in a social media website this trigger is outputted. I am trying to output a temporary trigger so that as soon they are friend in the output view a statement is shown. What is the syntax error exactly on Output in the following code please? If there could be improvement to the code inform me pls.

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

Upvotes: 0

Views: 283

Answers (1)

granadaCoder
granadaCoder

Reputation: 27862

  1. I think you want the PRINT statement:

    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;
    
        Print 'Trigger : '+@FriendA+' and '+@FriendB+' become friends.'
        END
    GO
    
  2. TRIGGERS ARE SET BASED. Do NOT apply "row by row" logic in a trigger.

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

Upvotes: 1

Related Questions