T-SQL @@ERROR Handling beginners Problem...

  • Hello!

    I need to check whether an UPDATE statement succeeded or not... But this code does not what it is supposed to be doing 😉

    UPDATE PWR_NODE_T

    SET MLOAD_WATTS = @WATTS_C

    WHERE (NODE_ORDINAL = 3) AND PO_VOB_ID

    IN

    (SELECT VOB_ID

    FROM PWR_BREAKER_T

    WHERE ([BREAKER_NAME] like @OBJECT_NAME) AND HOLD_DEVICE_VOB_ID

    IN

    (SELECT VOB_ID

    FROM PWR_PANEL_T

    WHERE ([PANEL_NAME] like @BREAKER_PANEL) AND HOLD_DEVICE_VOB_ID

    IN

    (SELECT VOB_ID

    FROM DV_DEVICE_T

    WHERE (([DEVICE_NAME] like @BREAKER_PDU_RPP) AND ([BUILDING] like @BUILDING_NAME) AND ([FLOOR] like @FLOOR) AND ([SPACE_ID] like @SPACE))

    )

    )

    )

    IF @@ERROR <> 0

    SET @UPDATE_ERROR = 2

    Please help 😉

    best regards

    lacky

  • I solved this one 😉

    An UPDATE does not spawn en error if no line is changed. I just had to check

    IF @@ROWCOUNT = 0

    /lackyg

  • Did it generate an error or did the update not occur? If you're checking for an update working, you'd want to look at the @@ROWCount instead of @@Error. Also, since you're in SQL SErver 2005, you can use TRY/CATCH instead of @@Error. It's more efficient.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • great minds think alike 😉

  • Nice work.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply