title: How to use a table variable in an "update from select" query?
body: <p>I have this table variable declaration followed by a query:</p>
  <pre><code>DECLARE @CurrentItems TABLE
  (
      ItemId uniqueidentifier,
      ItemUnits int
  )
  
  UPDATE U SET U.Units = U.Units + [@CurrentItems].ItemUnits
      FROM @CurrentItems CI INNER JOIN U ON U.UId=CI.ItemId;
  </code></pre>
  <p>And <code>U</code> is defined as follows:</p>
  <pre><code>CREATE TABLE [dbo].[U] (
      [UId]         UNIQUEIDENTIFIER UNIQUE NOT NULL,
      [Units]       INT DEFAULT ((0)) NOT NULL
  );
  </code></pre>
  <p>When I run that in SQL Management Studio against SQL Server 2005 Express I get the following:</p>
  <blockquote>
  <p>Msg 208, Level 16, State 1, Line 24</p>
  <p>Invalid object name '@CurrentItems'.</p>
  </blockquote>
  <p>I've already looked through <a href="https://stackoverflow.com/q/1499172/57428">this</a> and <a href="https://stackoverflow.com/q/4252394/57428">this</a> very similar questions but can't figure out how to solve the problem.</p>
  <p>What's the actual problem and how do I resolve that?</p>