此SQL查询有什么问题?
SELECT id AS Expr
FROM EXTDATA.Monitoring.dbo.Exceptions
WHERE EXTDATA.Monitoring.dbo.Exceptions.exceptionID = EXTDATA.Monitoring.dbo.RoomView.exceptionID
当我尝试执行查询时,收到此错误消息:
Msg 4104, Level ##, State 1, Line 3
The multi-part identifier "EXTDATA.Monitoring.dbo.Exceptions.exceptionID" could not be bound.
Msg 4104, Level ##, State 1, Line 3
The multi-part identifier "EXTDATA.Monitoring.dbo.RoomView.exceptionID" could not be bound.
RoomView是一个视图,Exceptions是一个表。
答案 0 :(得分:2)
加入后似乎是你
SELECT id AS Expr
FROM EXTDATA.Monitoring.dbo.Exceptions EX
INNER JOIN EXTDATA.Monitoring.dbo.RoomView RW ON EX.exceptionID = RW.exceptionID
答案 1 :(得分:2)
问题是查询的EXTDATA.Monitoring.dbo.RoomView
或FROM
子句中未提及JOIN
视图。您无法在WHERE
子句表达式中引用视图或表格:您需要添加EXISTS
或JOIN
:
SELECT id AS Expr
FROM EXTDATA.Monitoring.dbo.Exceptions e
WHERE EXISTS (
SELECT * FROM EXTDATA.Monitoring.dbo.RoomView v
WHERE e.exceptionID = v.exceptionID
)
或
SELECT DISTINCT id AS Expr
FROM EXTDATA.Monitoring.dbo.Exceptions e
JOIN EXTDATA.Monitoring.dbo.RoomView v ON e.exceptionID = v.exceptionID