title: Can not get @@ERROR after EXEC() with Error
body: <p>Please see the following t-sql code</p>
  
  <pre><code>   DECLARE @iError INT
     EXEC('select * from sysobj')
     SELECT @iError = @@ERROR
     PRINT 'Error = ' + CAST(@iError AS VARCHAR(10))
  </code></pre>
  
  <p>After I run it, it returns error message, which is what I want.</p>
  
  <blockquote>
    <p>Msg 208, Level 16, State 1, Line 1<br>
    Invalid object name 'sysobj'.<br>
    Error = 208</p>
  </blockquote>
  
  <p>However, if I change the query </p>
  
  <pre><code>   DECLARE @iError INT
     EXEC('select * from sysobjects where ''c'' = 1')
     SELECT @iError = @@ERROR
     PRINT 'Error = ' + CAST(@iError AS VARCHAR(10))
  </code></pre>
  
  <p>The output will be</p>
  
  <blockquote>
    <p>Msg 245, Level 16, State 1, Line 1<br>
    Conversion failed when converting the varchar value 'c' to data type int.</p>
  </blockquote>
  
  <p>The problem is any code afer EXEC() is not executed. </p>
  
  <p>In the real stored procedure, I have some code to handle the errors after PRINT. And none of those code executed in second case.</p>
  
  <p>Could someone let me know why this happens?</p>
  
  <p>I tested it on both SQL Server 2008 and 2005.</p>
  
  <p>Thanks</p>