title: How does SET NOEXEC ON work?
body: <p>I have the following code:</p>
  
  <pre><code>-- start of code
  set noexec off
  
  declare @requiredVersion int
  declare @currentVersion int
  
  set @requiredVersion = 5
  set @currentVersion = 4
  
  if (@currentVersion &lt; @requiredVersion)
  begin
      print 'Please update your DB to version 5 before running this script.'
      set noexec on
  end
  
  go
  -- print 'Dummy'
  insert into tblFooBar(name) values ('AAA')
  go
  
  set noexec off
  -- end of code
  </code></pre>
  
  <p>Please note that table "tblfoobar" does not exists in the database.  When I run this code, the message comes up:</p>
  
  <pre>
  Please update your DB to version 5 before running this script.
  Msg 208, Level 16, State 1, Line 1
  Invalid object name 'tblFooBar'.
  </pre>
  
  <p>I was expecting that setting up noexec to ON may not give the "Msg 208" part of the message.  </p>
  
  <p>Then again "set noexec on" compiles the code, does not execute it.  Trying to insert something into a table that does not exists is a compile-time error - I am guessing.  If that is the case, then the error  about "missing object" should come up.</p>
  
  <p>Now let me tell you the strange behaviour I have observed.  If I remove the comment from the line "-- Print 'dummy'"</p>
  
  <pre><code>-- start of code
  set noexec off
  
  declare @requiredVersion int
  declare @currentVersion int
  
  set @requiredVersion = 5
  set @currentVersion = 4
  
  if (@currentVersion &lt; @requiredVersion)
  begin
      print 'Please update your DB to version 5 before running this script.'
      set noexec on
  end
  go
  print 'Dummy'
  insert into tblFooBar(name) values ('AAA')
  go
  </code></pre>
  
  <p>and execute the code, I only get the following message.</p>
  
  <blockquote>
    <p>Please update your DB to version 5 before running this script.</p>
  </blockquote>
  
  <p>This time there is no message about missing table.</p>
  
  <p>Can someone please explain this behaviour to me?  Thanks.</p>