我在 Sql Server (2008) 中提交以下查询
WITH query AS (SELECT TOP 100 PERCENT
ROW_NUMBER() OVER
(ORDER BY [tbl2].[col2] ASC) AS TableRowNumber ,
[tbl1].[col1] ,
[tbl2].[col2]
FROM [db1].[dbo].[tbl1] AS [tbl1]
JOIN [db2].[dbo].[tbl2] AS [tbl2]
ON [tbl1].[id] = [tbl2].[id])
SELECT
*
FROM query
WHERE TableRowNumber BETWEEN 1 AND 15
ORDER BY TableRowNumber ASC
运行此查询时,它会返回以下错误消息:
Msg 207, Level 16, State 1, Line 3
Invalid column name 'col2'.
sql 连接本身运行良好(当作为普通查询运行时。问题似乎在于使用Row_Number() over (Order By COLUMN)
when与查询表COLUMN
位于不同的数据库中。FROM
如果我将第 3 行更改为读取,(Order By [tbl1].[col1] ASC)
那么它可以毫无问题地运行。仅当排序列位于不同的数据库中时才会发生该错误。
有谁知道为什么会这样?对于如何解决这个问题,有任何的建议吗?