Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Most likely you have a statement in between the error-causing statement and the <code>IF</code> check. Simplifying the error handling logic here (since it is an extremely vast topic) and ignoring batch-aborting and connection-terminating errors...</p> <p>This yields the error message and <code>'Uh oh'</code>:</p> <pre><code>SELECT 1/0; PRINT 'Uh oh'; IF @@ERROR &lt;&gt; 0 BEGIN PRINT 'Error.'; END </code></pre> <p>Since <code>@@ERROR</code> gets reset after every statement, it is no longer non-zero, and the <code>IF</code> block does not get entered. While the following works as expected, because we are checking <code>@@ERROR</code> immediately after the trouble statement:</p> <pre><code>SELECT 1/0; IF @@ERROR &lt;&gt; 0 BEGIN PRINT 'Error.'; END </code></pre> <p>If you do have stuff you need to do in between, you can store the value in a variable:</p> <pre><code>DECLARE @err INT; SELECT 1/0; SELECT @err = @@ERROR; PRINT 'Uh oh'; IF @err &lt;&gt; 0 BEGIN PRINT 'Error.'; END </code></pre> <p>This will execute both <code>PRINT</code> commands.</p> <p><strong>EDIT</strong></p> <p>Since you now added your code, I can give further insight into why this isn't working in your case. Invalid object is a severity 16 error:</p> <blockquote> <p>Msg 208, Level 16, State 1, Procedure procx, Line 110 <br>Invalid object name 'test'.</p> </blockquote> <p>Now, according to <a href="http://technet.microsoft.com/en-us/library/aa937483%28v=sql.80%29.aspx" rel="nofollow">the documentation</a>, severity 16 does not abort the batch (only 19 and above), but that doesn't mean this is always true. For example, see <a href="http://blogs.msdn.com/b/sqlserverfaq/archive/2011/05/11/errors-raised-with-severity-level-16-may-cause-transactions-into-doomed-state.aspx" rel="nofollow">this blog post</a> which explains that in some cases sev16 <em>can</em> abort the batch. I don't have a 2000 instance anywhere around to test this for certain or to investigate workarounds, other than (a) not creating stored procedures that reference objects that don't exist or (b) upgrading to a modern version of SQL Server that has put a little more thought into error handling).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload