Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Errors That stop execution of SP.
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Amy  
View profile  
 More options Jul 14 2003, 11:29 am
Newsgroups: microsoft.public.sqlserver.programming
From: "Amy" <l...@usa.com>
Date: Mon, 14 Jul 2003 18:29:18 +0200
Local: Mon, Jul 14 2003 12:29 pm
Subject: Errors That stop execution of SP.
Hello,

I found that referring to a table that does not exist or is inaccessible
from a SP stops the execution of the SP and returns the error.

select * from <nonexists>
OR
select * from <linked server>.db.owner.table     when the linked server is
inaccessible for some reason.

This prevents me from handling the error inside the SP code.

I found that i can solve it by using dynamic execution - EXEC
(select......).

I have 3 questions :

1.    Is there a better way to handle this situation ?
2.    Are there any more error situation that cause the SP to stop execution
and return ?
3     Do the same rules apply to UDF and Triggers ?

Thanks,

Amy


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacco Schalkwijk  
View profile  
 More options Jul 14 2003, 12:16 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Jacco Schalkwijk" <NOSPAMjac...@eurostop.co.uk>
Date: Mon, 14 Jul 2003 17:13:10 +0100
Local: Mon, Jul 14 2003 12:13 pm
Subject: Re: Errors That stop execution of SP.
1. Nope. You should know in advance however if a table (or column) exists or
not. You can always find out with something like IF EXISTS(SELECT * FROM
information_schema.tables where table_name = <table_name>, but basically
your database schema should be stable and tables shouldn't just disappear. A
linked server being unreachable is a runtime error, and you can't prepare
for that.

2. Yes, any error returned by something outside SQL Server will terminate
the batch, i.e. stop the stored procedure execution. This includes BULK
INSERT, extended procedures, calling COM objects with the sp_OA% stored
procedures, etc. Also if you try to access columns that do not exists this
will terminate the batch as well.

3. Triggers and UDF's are worse. You can't trap _any_ errors inside them.
Errors in UDF's and trigger will terminate the batch and rollback any open
transactions. As UDF's and triggers are supposed to work like normal
database functions, tables and constraints that makes a lot of sense,
because SQL Server does not know with the current error handling if an error
is actually handled, and having a possibly unhandled error in what should be
an atomic, consistent transaction is a no-go.

--
Jacco Schalkwijk MCDBA, MCSD, MCSE
Database Administrator
Eurostop Ltd.

"Amy" <l...@usa.com> wrote in message

news:uV95wxhSDHA.2196@TK2MSFTNGP12.phx.gbl...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erland Sommarskog  
View profile  
 More options Jul 15 2003, 6:43 pm
Newsgroups: microsoft.public.sqlserver.programming
From: Erland Sommarskog <som...@algonet.se>
Date: Tue, 15 Jul 2003 22:44:42 +0000 (UTC)
Local: Tues, Jul 15 2003 6:44 pm
Subject: Re: Errors That stop execution of SP.

Jacco Schalkwijk (NOSPAMjac...@eurostop.co.uk) writes:
> Also if you try to access columns that do not exists this
> will terminate the batch as well.

No, access to a non-existing column that was not detected when you loaded
the procedure because of deferred name resolution will not abort the
batch. Execution of the current scope is aborted, but execution continues
in the calling scope. Note that if the aborted procedure started a
transaction, this transaction is not rolled back.

This applies to all compilation errors that occurs during run-time.

> 3. Triggers and UDF's are worse. You can't trap _any_ errors inside
> them. Errors in UDF's and trigger will terminate the batch and rollback
> any open transactions. As UDF's and triggers are supposed to work like
> normal database functions, tables and constraints that makes a lot of
> sense, because SQL Server does not know with the current error handling
> if an error is actually handled, and having a possibly unhandled error
> in what should be an atomic, consistent transaction is a no-go.

When an error occurs in a trigger, or in some code invoked by the trigger,
execution is aborted immediately, and the transaction is rolled back. With
two exception: RAISERROR does not terminate the batch, and neither does
error 266, mismatch in @@trancount, because this error is simply not raised
at all when in trigger context.

UDFs are not like that. They work like stored procedures. That is, if the
error is one that aborts the batch is aborted, else execution continues.
Except that, when a UDF is invoked as part of an INSERT, UPDATE, DELETE
SELECT and SET statement, that statement is terminated immediately, so
you cannot trap the error inside the UDF. Worse is, you cannot trap
the error after the aborted statement either, because at that point
@@error is 0! (This applies for scalar UDF and multi-statement UDF, not
for inline UDF. For them @@error is set.)

If you invoke a UDF with EXEC (possible with scalar UDF), execution
continues in case of a such an error, and you can inspect @@error to
find out.

--
Erland Sommarskog, SQL Server MVP, som...@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erland Sommarskog  
View profile  
 More options Jul 15 2003, 6:46 pm
Newsgroups: microsoft.public.sqlserver.programming
From: Erland Sommarskog <som...@algonet.se>
Date: Tue, 15 Jul 2003 22:48:03 +0000 (UTC)
Local: Tues, Jul 15 2003 6:48 pm
Subject: Re: Errors That stop execution of SP.

Amy (l...@usa.com) writes:
> 1.    Is there a better way to handle this situation ?

Yes. Load your procedures with AbaPerls, then you will be alerted when
you load the procedures, that the tables are missing. AbaPerls is a toolset
that we use in our shop, and that I have made available as freeware. You
find it on http://www.abaris.se/abaperls(

> 2.    Are there any more error situation that cause the SP to stop
> execution and return ?

All compilations errors, such missing column, missing alias etc that were
not detected when you loaded the procedure, causes the procedure to abort
and execution continue in the caller.

There is also a set of errors that causes the entire batch to be
aborted, and any out-standing transaction to be rolled back.

> 3     Do the same rules apply to UDF and Triggers ?

Yes. Except that any error in a trigger causes the batch to be aborted
immediately.

--
Erland Sommarskog, SQL Server MVP, som...@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacco Schalkwijk  
View profile  
 More options Jul 16 2003, 4:52 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Jacco Schalkwijk" <NOSPAMjac...@eurostop.co.uk>
Date: Wed, 16 Jul 2003 21:54:48 +0100
Local: Wed, Jul 16 2003 4:54 pm
Subject: Re: Errors That stop execution of SP.
Hi Erland,

Thanks for your corrections. I didn't have a clear distinction between scope
and batch, that is what made my answer incorrect. It is good to see this
information summed up, because it is very hard to come by.

Regards,

Jacco

"Erland Sommarskog" <som...@algonet.se> wrote in message

news:Xns93BA7558BE4EYazorman@127.0.0.1...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google