0

I am trying to update a data set based on one conditional and then retrieving all the updated rows. VS keeps telling me there is an incorrect syntax error near my OUTPUT clause but I do not see anything wrong. I am just trying to figure out how to use "OUTPUT" so this may be a very stupid mistake I am making but failing to see.

What is wrong (syntactically) with this OUTPUT clause?

CREATE PROCEDURE [dbo].[GetInitialSessionNotifications]
@CurrentSessionId bigint
AS
DECLARE @tempTable table(
id bigint NOT NULL,
[Type] nvarchar,
DocumentCommentID bigint,
AnnouncmentID int,
EventID int,
MeetingID int,
[Read] bit,
RecieverId int,
AnnouncmentCommentId bigint,
EventCommentId bigint,
MeetingCommentId bigint,
DateAndTime DateTime);

UPDATE Notifications SET SessionId = @CurrentSessionId
WHERE SessionId != @CurrentSessionId
OUTPUT INSERTED.id,
INSERTED.[Type],
INSERTED.DocumentCommentID,
INSERTED.AnnouncmentID,
INSERTED.EventID,
INSERTED.MeetingID,
INSERTED.[Read],
INSERTED.RecieverId,
INSERTED.AnnouncmentCommentId,
INSERTED.EventCommentId,
INSERTED.MeetingCommentId,
INSERTED.DateAndTime
INTO @tempTable;

SELECT id, [Type], DocumentCommentId, AnnouncmentID, EventID, MeetingID,
[Read], RecieverId, AnnouncmentCommentId, EventCommentId, MeetingCommentId, DateAndTime
FROM @tempTable;

RETURN 0

8
  • 3
    WHERE goes after OUTPUT. Commented May 12, 2013 at 17:48
  • 1
    @Adrian Exactly, UPDATE Notifications SET SessionId = @CurrentSessionId OUTPUT INSERTED.id, INSERTED.[Type], ... INSERTED.DateAndTime INTO @tempTable WHERE SessionId != @CurrentSessionId; Commented May 12, 2013 at 17:52
  • 1
    Also: you should always define a length when declaring a (n)varchar column! So this here: [Type] nvarchar, might end up giving you a 1 character long string column - is that what you want? Commented May 12, 2013 at 18:31
  • 2
    From Books Online: UPDATE ... [ <OUTPUT Clause> ] [ FROM{ <table_source> } [ ,...n ] ] [ WHERE { <search_condition> ... Commented May 12, 2013 at 18:54
  • 1
    @BogdanSahlean I'm still trying to wrap my head around complex sql queries. I have been teaching myself for the past few years and was used to the simple select, update, and insert queries. Now its getting more exciting that I know how to use joins, batch queries and procession with in sql server. Commented May 13, 2013 at 0:22

1 Answer 1

0

Try this one -

CREATE PROCEDURE [dbo].[GetInitialSessionNotifications]

@CurrentSessionId BIGINT

AS BEGIN

    DECLARE @tempTable TABLE
    (
        id BIGINT NOT NULL ,
        [Type] NVARCHAR ,
        DocumentCommentID BIGINT ,
        AnnouncmentID INT ,
        EventID INT ,
        MeetingID INT ,
        [Read] BIT ,
        RecieverId INT ,
        AnnouncmentCommentId BIGINT ,
        EventCommentId BIGINT ,
        MeetingCommentId BIGINT ,
        DateAndTime DATETIME
    )

    UPDATE  Notifications
    SET     SessionId = @CurrentSessionId
    OUTPUT  
        INSERTED.id ,
        INSERTED.[Type] ,
        INSERTED.DocumentCommentID ,
        INSERTED.AnnouncmentID ,
        INSERTED.EventID ,
        INSERTED.MeetingID ,
        INSERTED.[Read] ,
        INSERTED.RecieverId ,
        INSERTED.AnnouncmentCommentId ,
        INSERTED.EventCommentId ,
        INSERTED.MeetingCommentId ,
        INSERTED.DateAndTime
    INTO @tempTable
    WHERE   SessionId != @CurrentSessionId

    SELECT  id ,
            [Type] ,
            DocumentCommentId ,
            AnnouncmentID ,
            EventID ,
            MeetingID ,
            [Read] ,
            RecieverId ,
            AnnouncmentCommentId ,
            EventCommentId ,
            MeetingCommentId ,
            DateAndTime
    FROM    @tempTable;

END
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.