SQL 错误 无法绑定多部分标识符
Posted
技术标签:
【中文标题】SQL 错误 无法绑定多部分标识符【英文标题】:SQL Error The multi-part identifier could not be bound 【发布时间】:2014-09-29 05:16:27 【问题描述】:当我尝试执行此查询时,我收到“无法绑定多部分标识符“#EachEmployee.ResultID”。”错误。
DECLARE @QueryText VARCHAR(1000)
SET @QueryText = '
UPDATE #EachEmployee2
SET #EachEmployee2.CorrectAnswerCount = (
SELECT COUNT (TMID)
FROM
' + @WorkDatabaseName + '.dbo.TestBlockTM AS TBTM,
' + @WorkDatabaseName + '.dbo.TestResultTM AS TRTM
WHERE 1 = 1
AND TBTM.TMID = TRTM.OtvetID
AND TBTM.TMPID = TRTM.VoprosID
AND TRTM.TestResultID = #EachEmployee2.ResultID
)
WHERE
#EachEmployee2.IsGroup = 0 AND #EachEmployee2.BlockID = 1'
EXECUTE(@QueryText)
但是,类似的查询运行良好:
UPDATE #EachEmployee2
SET #EachEmployee2.ResultID = (
SELECT TOP 1 TestResultID
FROM #AnswersList AS a
WHERE 1 = 1
AND a.SID = #EachEmployee2.SID
AND a.UserID = #EachEmployee2.UserID
)
有人能说出这里有什么问题吗?感谢并感谢您的帮助。
【问题讨论】:
【参考方案1】:试试这个
SET @QueryText = '
UPDATE emp
SET emp.CorrectAnswerCount = (
SELECT COUNT (TMID)
FROM
' + @WorkDatabaseName + '.dbo.TestBlockTM AS TBTM,
' + @WorkDatabaseName + '.dbo.TestResultTM AS TRTM
WHERE 1 = 1
AND TBTM.TMID = TRTM.OtvetID
AND TBTM.TMPID = TRTM.VoprosID
AND TRTM.TestResultID = emp.ResultID
)
From #EachEmployee2 emp
WHERE
emp.IsGroup = 0 AND emp.BlockID = 1'
【讨论】:
这很有帮助。老实说,我不知道你可以在“UPDATE..SET..”表达式之后使用 FROM。感谢您的帮助。【参考方案2】:你能试试这个查询并告诉我错误是否仍然发生:
SET @QueryText = '
UPDATE #EachEmployee2
SET CorrectAnswerCount = (
SELECT COUNT (TMID)
FROM
' + @WorkDatabaseName + '.dbo.TestBlockTM AS TBTM,
INNER JOIN ' + @WorkDatabaseName + '.dbo.TestResultTM AS TRTM ON TRTM.OtvetID = TBTM.TMID
AND TRTM.VoprosID = TBTM.TMPID
AND TRTM.TestResultID = E.ResultID
)
FROM #EachEmployee2 E
WHERE E.IsGroup = 0
AND E.BlockID = 1'
我添加了FROM
子句并稍微修改了子查询,以便用显式的INNER JOIN
子句替换旧的SQL 语法。
希望这会有所帮助
【讨论】:
以上是关于SQL 错误 无法绑定多部分标识符的主要内容,如果未能解决你的问题,请参考以下文章