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.
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" <= "LTV"."Ref_Key"
GROUP BY "COUNTER"."Collateral_ID", "COUNTER"."m_il_no"
) "MIL_IDX"
FROM @t AS LTV