title: Confused by SCOPE_IDENTITY() and GO
body: <p>I am a bit confused by the documentation &amp; behavior of SCOPE_IDENTITY() in SQL Server. </p>
  
  <p>This page <a href="https://docs.microsoft.com/en-us/sql/t-sql/functions/scope-identity-transact-sql?view=sql-server-2017" rel="noreferrer">https://docs.microsoft.com/en-us/sql/t-sql/functions/scope-identity-transact-sql?view=sql-server-2017</a> says this about SCOPE_IDENTITY():</p>
  
  <blockquote>
    <p>Returns the last identity value inserted into an identity column in
    the same scope. A scope is a module: a stored procedure, trigger,
    function, or <strong>batch</strong>. Therefore, if two statements are in the same
    stored procedure, function, or batch, they are in the same scope.</p>
  </blockquote>
  
  <p>And it contains this example </p>
  
  <pre><code>USE AdventureWorks2012;  
  GO  
  INSERT INTO Person.ContactType ([Name]) VALUES ('Assistant to the Manager');  
  GO  
  SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY];  
  GO  
  SELECT @@IDENTITY AS [@@IDENTITY];  
  GO  
  </code></pre>
  
  <p>Which returns </p>
  
  <pre><code>SCOPE_IDENTITY  
  21  
  @@IDENTITY  
  21
  </code></pre>
  
  <p>From the docs I would have thought that the result of SCOPE_IDENTITY() would be NULL because SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]; is executed in a different <strong>batch</strong> (because it comes after GO) than the INSERT command... What am I missing here? </p>