Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
@@Error question
Message
General information
Forum:
Microsoft SQL Server
Category:
Other
Title:
@@Error question
Miscellaneous
Thread ID:
01038542
Message ID:
01038542
Views:
73
The Books Online for @@Error states the following:
@@ERROR
Returns the error number for the last Transact-SQL statement executed.

Syntax
@@ERROR

Return Types
integer

Remarks
When Microsoft® SQL Server™ completes the execution of a Transact-SQL 
statement, @@ERROR is set to 0 if the statement executed successfully. If an 
error occurs, an error message is returned. @@ERROR returns the number of 
the error message until another Transact-SQL statement is executed. You can 
view the text associated with an @@ERROR error number in the sysmessages 
system table.

Because @@ERROR is cleared and reset on each statement executed, check it 
immediately following the statement validated, or save it to a local 
variable that can be checked later.
It is even so kind as to give an example of how to check the statement to be validated directly after it is executed.
INSERT authors
   VALUES('409-56-7008', 'Bennet', 'Abraham', '415 658-9932',
   '6223 Bateman St.', 'Berkeley', 'CA', '94705', 1)
-- Set a variable to the error value for 
-- the INSERT statement.
SELECT @ins_error = @@ERROR
Sweet of them, don't ya think? The problem I am having is that I cannot duplicate this behavior in my own insert trigger. The code for which is as follows:
CREATE TRIGGER T_I_CreateStatementOfWork ON [dbo].[drfAssignment] 

FOR INSERT

AS

DECLARE @cSOWNumber       CHAR(7),
        @iSOWID           INT, 
        @iAssignmentFK    INT, 
        @iSOWNumber_Error INT,
        @iSOWID_Error     INT,
        @iInsert_Error    INT,
        @Error_Number     INT,
        @Error_Message    CHAR(100)

   /* Every time an assignment is successfully created, automatically create a child Statement of Work
      whose purpose is to begin collecting billing information from the investigators. */


   EXEC @cSOWNumber = mm_newid 'DRFSOWNUMBER'
   SELECT @iSOWNumber_Error = @@ERROR

   EXEC @iSOWID = mm_newid 'DRFSOW'
   SELECT @iSOWID_Error = @@ERROR

   SET @iAssignmentFK = (SELECT iID from inserted)

   INSERT INTO drfStatementOfWork ( iid, 
                                    csowno, 
                                    iAssignmentFK) 
                           VALUES ( @iSOWID, 
                                    @cSOWNumber, 
                                    @iAssignmentFK)

   SELECT @iInsert_Error = @@ERROR


IF @iSOWNumber_Error > 0 OR @iSOWID_Error > 0 OR @iInsert_Error > 0
	BEGIN
           IF @iSOWNumber_Error <> 0
              BEGIN
                 SELECT @error_number=30005,
                        @error_message='Error generating the new SOW Number ... transaction rolled back.'
                 GOTO error
              END
           IF @iSOWID_Error <> 0
              BEGIN
                 SELECT @error_number=30005,
                        @error_message='Error generating the new SOW ID ... transaction rolled back.'
                 GOTO error
              END
           IF @iInsert_Error <> 0
              BEGIN
                 SELECT @error_number=30005,
                        @error_message='Error generating the new SOW ... transaction rolled back.'
                 GOTO error
              END
        END 
RETURN

/*  Error Handling  */
error:
RAISERROR @error_number @error_message
ROLLBACK TRANSACTION
Anyone care to point out the nature of my cranial/rectal inversion?

Thanks,

CTBlankenship
Next
Reply
Map
View

Click here to load this message in the networking platform