在T-SQL中,如何在子查询中引用表变量?
我已经声明了一个表变量“@t”,并正确执行了“INSERT-INTO-SELECT”。 当我尝试对每组行编号进行一些额外计算来查询表变量时,出现错误“直接使用“@t”时必须声明变量”或“使用“@t”别名时“无效的对象名称” '。请各位指教。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用:
Use: