compiled. The following compiles nicely:
Compiles nicely. Try to run it:
interested in consulting services. All other replies will be ignored :)
"Greg Linwood" <g_linwood@hotmail.com> wrote in message
news:%23ThQG4%23iDHA.220@tk2msftngp13.phx.gbl...
> Might not actually be a compile error if the stored proc was compiled when
> the table existed..
>
> I didn't notice it was an object not found type error in your original
post,
> sorry.. This error is about the most difficult to handle. It never was
such
> a big problem in the early days - it became a problem in SQL 7 when
deferred
> name resolution was introduced (although I'm not 100% sure this is the
> cause).
>
> Basically, as Jacco says - there is no way you can handle an object not
> found error in T-SQL when a table is referenced that doesn't exist because
> execution ceases unconditionally, so any error handling you have after
that
> line simply won't get executed. Additionally, if you have a transaction
> open, it will be left open - even if you use SET XACT_ABORT ON which is
> another thing to look out for. Generally though - if you want to use error
> handling in T-SQL, you need to check @@error after every line..
>
> I've always personally been against sticking big fat, branching checks for
> table existance in the code path because it causes an unfortunate
> concurrency problem on sysobjects (during the object_id() check). It also
> contributes to making code seem more like spaghetti code which complicates
> maintenance. Personally, I (like many others) wish the exception
management
> was better & am looking forward to something better in the next release.
>
> My best answer to solving this problem is to put your defensive code into
> the application layer - ie checking for object existance if need be there.
> My rational for this is that the application layer needs to be cleaning up
> transactions left over by object not found type errors anyway..
>
> HTH
>
> Regards,
> Greg Linwood
> SQL Server MVP
>
> "Jacco Schalkwijk" <NOSPAMjaccos@eurostop.co.uk> wrote in message
> news:uo6$Tj#iDHA.2728@TK2MSFTNGP10.phx.gbl...
> > Hi Sam,
> >
> > Compile errors, like for example the fact that a (temporary) table
doesn't
> > exists, will terminate the batch and any statement after the statement
> that
> > caused the error will not be executed, so your errorhandler won't run.
You
> > can check for the existence of the temporary table before you do the
> insert
> > with
> > IF OBJECT_ID('tempdb..#manageril1') IS NOT NULL
> > insert into manager select * from #manageril1
> > ELSE --Oops no table!
> > GOTO error_handler
> >
> > But basically it is a big fat bug in your code that the table doesn't
> exist,
> > and I am of the opinion that in a database you should test for bugs and
> not
> > go out of your way to write error handling code for them. The only thing
> > error handling code in this specific case can do is raise a message, you
> > can't recover from it.
> >
> > --
> > Jacco Schalkwijk
> > SQL Server MVP
> >
> >
> > "Sam" <samuel.soong@sgprivasia.com> wrote in message
> > news:116401c38be7$3154e7a0$a101280a@phx.gbl...
> > > hi!
> > > I have tried your method. Doesn't help.
> > > I am still getting messages like:
> > > ====================
> > > transaction starts
> > > (10 row(s) affected)
> > > (12 row(s) affected)
> > > (13 row(s) affected)
> > > Server: Msg 208, Level 16, State 1, Line 9
> > > Invalid object name 'manageril1'.
> > > =====================
> > > the procedure did not even reach the error_handler step:
> > >
> > > begin tran
> > > print 'transaction starts'
> > > insert into profile select * from #profins
> > > if @@error !=0 GOTO error_handler
> > > insert into prof_compo select * from #compoins
> > > if @@error !=0 GOTO error_handler
> > > insert into apps_user select * from #appluserins
> > > if @@error !=0 GOTO error_handler
> > > insert into manager select * from #manageril1
> > > if @@error !=0 GOTO error_handler
> > >
> > > COMMIT TRAN
> > > RETURN
> > >
> > > error_handler:
> > > print 'before rollback'
> > > rollback tran
> > > print 'rollback'
> > >
> > > return
> > > go
> > > >-----Original Message-----
> > > >Hi Sam.
> > > >
> > > >You have to check @@error after EVERY statement in T-SQL.
> > > >
> > > >Try this:
> > > >
> > > >begin tran
> > > >print 'Here goes the transactions'
> > > >insert into profile select * from #profins
> > > >if @@error !=0 goto error_handler
> > > >insert into prof_compo select * from #compoins
> > > >if @@error !=0 goto error_handler
> > > >insert into apps_user select * from #appluserins
> > > >if @@error !=0 goto error_handler
> > > >insert into manager select * from #manageril1
> > > >if @@error !=0 goto error_handler
> > > >
> > > >commit tran
> > > >return
> > > >
> > > >error_handler:
> > > > rollback tran
> > > > print 'rollback'
> > > >
> > > >return
> > > >go
> > > >
> > > >You can also use SET XACT_ABORT ON to auto-rollback a
> > > transaction on any
> > > >error, but be aware - there are pit-falls to using this &
> > > documentation is
> > > >fairly skinny on how it all works.
> > > >
> > > >I strongly suggest you read two articles on T-SQL error
> > > handling written by
> > > >SQL MVP Erland Sommarskog a few months back. T-SQL error
> > > handling is far
> > > >more complex than it first looks & you'd do very well to
> > > read these articles
> > > >at:
> > > >
http://www.algonet.se/~sommar/
> > > >
> > > >HTH
> > > >
> > > >Regards,
> > > >Greg Linwood
> > > >SQL Server MVP
> > > >
> > > >"Sam" <samuel.soong@sgprivasia.com> wrote in message
> > > >news:0ffb01c38bd2$2b75a870$a101280a@phx.gbl...
> > > >> Error handling in TSQL
> > > >> I have the following codes in TSQL to run. I set the
> > > last
> > > >> insert statement to have error. But I did not get the
> > > >> rollback tran.
> > > >>
> > > >> What i got is:
> > > >> Here goes the transactions
> > > >> (10 rows)
> > > >> (12 rows)
> > > >> (13 rows)
> > > >> Server: Msg 208, Level 16, State 1, Line 6
> > > >> Invalid object name '#manageril1'.
> > > >> ======================================
> > > >> begin tran