Storing the value of the function @@Error


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
(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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
(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
(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')

FROM @Errs
WHERE errID != 0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adamtraskAuthor Commented:
Thanks bhess1...
This will come handy at some point...
adamtraskAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.