gpt4 book ai didi

SQL Server 2008 列前缀错误

转载 作者:行者123 更新时间:2023-12-01 09:14:55 26 4
gpt4 key购买 nike

所以我可以正常工作:

CREATE FUNCTION dbo.GetLiveStream(@UserName NVARCHAR(MAX)) RETURNS TABLE
AS
RETURN (
SELECT DISTINCT l.*
FROM StreamView l
JOIN Friendships f ON f.Sender = @UserName OR f.Recipient = @UserName
WHERE l.Sender <> @UserName
AND l.Recipient <> @UserName
AND ( l.Sender = f.Recipient
OR l.Sender = f.Sender
OR l.Recipient = f.Sender
OR l.Recipient = f.Recipient)

)
GO

但是当尝试执行以下查询时:

SELECT ROW_NUMBER() OVER(ORDER BY DateTime DESC) AS RowNumber, 
*
FROM dbo.GetLiveStream('jason')
WHERE RowNumber >= 0
AND RowNumber < 0+ 10;

我收到以下错误:

Msg 207, Level 16, State 1, Line 4
Invalid column name 'RowNumber'.
Msg 207, Level 16, State 1, Line 5
Invalid column name 'RowNumber'.

请注意,指定 l.* 会返回此错误:

Msg 107, Level 15, State 1, Line 2
The column prefix 'l' does not match with a table name or alias name used in the query.

GetLiveStream 是一个函数:

RETURN (
SELECT DISTINCT l.*
FROM StreamView l
JOIN Friendships f ON f.Sender = @UserName OR f.Recipient = @UserName
WHERE l.Sender <> @UserName
AND l.Recipient <> @UserName
AND ( l.Sender = f.Recipient
OR l.Sender = f.Sender
OR l.Recipient = f.Sender
OR l.Recipient = f.Recipient) AND f.IsPending = 0

)

如何解决?谢谢。

最佳答案

您不能在 WHERE 子句中引用别名。这样做:

select *
from
(
SELECT ROW_NUMBER() OVER(ORDER BY DateTime DESC) AS RowNumber,
*
FROM dbo.GetLiveStream('jason')
) a
WHERE RowNumber >= 0
AND RowNumber < 0+ 10;

至于您的 l.* 错误,那是因为该别名是函数及其查询定义的本地别名,无法在其外部引用。

关于SQL Server 2008 列前缀错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9338665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com