Storing the value of the function @@Error

Hi,


I am trying to see if I can store the values produced by function @@Error in a variable and, subsequently, print the value of the variable...

I understand that @@Error function automatically resets itself to 0 when it comes across a new line of code that is being evaluated, which means only the value of the last error is displayed but not the errors that comes before that.....  

In the example below the value of the variable @myVariable is printed as 0, but that's because no errors were found and because the function was only used once. Could you give me an example where the code produces more than one error and the value of each @@Error is printed

Declare @myVariable int
insert into Employee
Values
(62, 'Peter','Pan',GETDATE(),65,3,'Active','2002-08-12 00:00:00.0000000 -8:00','-8:00')
select @myVariable = @@Error
Print 'The value of Error is ' +  convert(varchar,@myVariable)

Thank you
adamtraskAsked:
Who is Participating?
 
Brendt HessSenior DBACommented:
I needed to track multiple errors some time ago, and this was the basic method I used

DECLARE @Errs table (
    ID int identity(1,1) primary Key,
    errID int not null,
    Note varchar(255) null
    )

insert into Employee
Values
(62, 'Peter','Pan',GETDATE(),65,3,'Active','2002-08-12 00:00:00.0000000 -8:00','-8:00')
INSERT INTO @Errs (errID, note) VALUES(@@Error, 'Insert Into Employee')

insert into Employee
Values
(63, 'Tinker','Bell',GETDATE(),65,3,'Active','2002-08-13 00:00:00.0000000 -8:00','-8:00')
INSERT INTO @Errs (errID, note) VALUES(@@Error, 'Insert Into Employee')

SELECT *
FROM @Errs
WHERE errID != 0
ORDER BY id
0
 
adamtraskAuthor Commented:
Thanks bhess1...
This will come handy at some point...
0
 
adamtraskAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.