Using @@Error in RaiseError

How do I use RAISEERROR such that it simply re-raises an error captured earlier?

BEGIN TRAN MyTrans
Declare @FailErr int, @NewID int

INSERT MyTableA(varA, VarB) VALUES (@VarA, @VarB)
     SET @FailErr=@@ERROR, @NewID=@@IDENTITY
     IF @FailErr <> 0 GOTO ErrrorOut
          ... more tsql ...
INSERT MyTableB(varA, VarB) VALUES (@VarA, @VarB)
     SET @FailErr=@@ERROR, @NewID=@@IDENTITY
     IF @FailErr <> 0 GOTO ErrrorOut
          ... more tsql ...
INSERT MyTableC(varA, VarB) VALUES (@VarA, @VarB)
     SET @FailErr=@@ERROR, @NewID=@@IDENTITY
     IF @FailErr <> 0 GOTO ErrrorOut
          ... more tsql ...

COMMIT TRAN MyTrans
goto ExitSP

ErrrorOut:
ROLLBACK TRAN MyTrans

ExitSP:
if @FailErr <> 0
    RAISEERROR (??????? I want to raise @FailErr ????????)
ELSE
    Select @FailErr
LVL 5
volkingAsked:
Who is Participating?
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.

Aneesh RetnakaranDatabase AdministratorCommented:
BEGIN TRAN MyTrans
Declare @FailErr int, @NewID int

INSERT MyTableA(varA, VarB) VALUES (@VarA, @VarB)
     SET @FailErr=@@ERROR, @NewID=SCOPE_IDENTITY()
     IF @FailErr <> 0 GOTO ErrrorOut


INSERT MyTableB(varA, VarB) VALUES (@VarA, @VarB)
     SET @FailErr=@@ERROR, @NewID=SCOPE_IDENTITY()
     IF @FailErr <> 0 GOTO ErrrorOut


INSERT MyTableC(varA, VarB) VALUES (@VarA, @VarB)
     SET @FailErr=@@ERROR, @NewID=SCOPE_IDENTITY()
     IF @FailErr <> 0 GOTO ErrrorOut


COMMIT TRAN MyTrans
goto ExitSP

ErrrorOut:
ROLLBACK TRAN MyTrans

ExitSP:
if @FailErr <> 0
  RAISERROR ('Some Error Message', 16, 1)
 ELSE
    Select @FailErr
0
volkingAuthor Commented:
@aneeshattingal
nope ... I want to raise whatever error was ACTUALLY captured in @FailErr
0
Aneesh RetnakaranDatabase AdministratorCommented:
I am not sure whether tht will work since RAISERROR accepts msgId's greater than 13000  and in case @@ERROR  is less than 13000, it will thrw some other error

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

volkingAuthor Commented:
@aneeshattingal - Your comment,  is exactly why I asked the question. I want to elegantly capture any errors but then Re-SPAWN the error later, after I've cleaned up. That way, my C# ADO call will receive exactly the error that actually happened.
0
Scott PletcherSenior DBACommented:
That's very hard to do in SQL Server 2000.

You can include the original error number in your error msg, but afaik you cannot trigger that specific error to re-occur.
0
volkingAuthor Commented:
@ScottPletcher

Ok forget elegant talk FUNCTIONAL. If my tsql starts a transaction then blindly does tsql and something fails (like foreign key violation, null into a non null field, etc) ... will the entire trans be automatically rolled back? Without my help? So, this is actually all I need?

BEGIN TRAN MyTrans
    INSERT MyTableA(varA, VarB) VALUES (@VarA, @VarB)
    INSERT MyTableB(varA, VarB) VALUES (@VarA, @VarB)
    INSERT MyTableC(varA, VarB) VALUES (@VarA, @VarB)
COMMIT TRAN MyTrans

And the rollback happens?
And my C# ADO will get the error?
0
Scott PletcherSenior DBACommented:
>> If my tsql starts a transaction then blindly does tsql and something fails (like foreign key violation, null into a non null field, etc) ... will the entire trans be automatically rolled back? Without my help? <<

No.  Unfortunately it depends on the specific error.  Certain errors cause an automatic failure [at least assuming no TRY ... CATCH], other errors do not.

The only safe way is to test for errors and rollback yourself, as you're doing now.
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
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

From novice to tech pro — start learning today.