title: In T-SQL, how to reference to table variable in the subquery?
body: <p>I've declared a table variable '@t', and have correctly performed the 'INSERT-INTO-SELECT'.
  When I was trying to query the table variable with some additional computation for per-group row numbering, I got error either "Must declare the variable" when using '@t' directly or "invalid object name" while using alias of '@t'. Please kindly advise.</p>
  
  <pre><code>SELECT 
      *,
      (SELECT COUNT(*) FROM "LTV" "COUNTER"
       WHERE 
          "COUNTER"."Collateral_ID" = "LTV"."Collateral_ID"
          AND
          "COUNTER"."m_il_no" = "LTV"."m_il_no"
          AND
          "COUNTER"."Ref_Key" &lt;= "LTV"."Ref_Key"
       GROUP BY "COUNTER"."Collateral_ID", "COUNTER"."m_il_no"
      ) "MIL_IDX"
  
  FROM @t AS LTV
  </code></pre>