title: Is is safe to use @@IDENTITY in a transaction?
body: <p>I was reading <a href="https://stackoverflow.com/a/42655/3680712">this</a> answer about different methods of getting the last identity value entered into a database.</p>
  
  <p>From what I understand, <code>@@IDENTITY</code> is usually a <strong>very bad idea</strong> because it might return an identity that is not the one you expected--for example an identity value that was recently created by a trigger. </p>
  
  <p>But what if your code is in a transaction?</p>
  
  <p>For example this is a simplified version of a transaction I'm doing (using  ColdFusion):</p>
  
  <pre><code>&lt;cftransaction&gt;
      &lt;cfquery name="queryInsertA" datasource="source"&gt;
          INSERT INTO tableA (columnName) VALUES (value)
      &lt;/cfquery&gt;
      &lt;cfquery name="queryInsertB" datasource="source"&gt;
          INSERT INTO tableB (fkey_tableA, columnName) VALUES (@@IDENTITY, value)
      &lt;/cfquery&gt;
  &lt;/cftransaction&gt;
  </code></pre>
  
  <p>Since, "<a href="https://msdn.microsoft.com/en-us/library/ms174377.aspx" rel="nofollow noreferrer">If a transaction is successful, all of the data modifications made during the transaction are committed and become a permanent part of the database</a>," does this mean that it would also prevent the isses that can arise when using <code>@@IDENTITY</code>? Or am I misunderstanding the behavior of transactions?</p>