为什么我不能设置变量来选择查询结果?我收到错误
' tbl'
附近的语法不正确
代码:
DECLARE @AgentNumber INT = 2428
DECLARE @Node HIERARCHYID
SET @Node = -- this is throwing me syntax error
(SELECT Node
FROM CM.ERGO_HIERARCHY
WHERE AgentNumber = @AgentNumber) tbl
我知道下面的代码有效,但为什么我不能简单地使用SET?
SELECT @Node = Node -- that works
FROM
(SELECT Node
FROM CM.ERGO_HIERARCHY
WHERE AgentNumber = @AgentNumber) tbl
答案 0 :(得分:1)
尝试改为:
SET @Node =
(
SELECT TOP 1 Node
FROM CM.ERGO_HIERARCHY
WHERE AgentNumber = @AgentNumber
)