所以我一直在努力:
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)
ORDER BY DateTime DESC
它非常适合获取所需记录的列表。 无论如何,我希望能够为这个查询提供两个新属性,从哪个行开始获取记录,以及第二个定义长度的整数。
原因是我正在申请“懒惰滚动”。对这些数据的影响。
我想将这两个值添加到查询中:(Int32 startAt,Int32 howMany)
有什么想法吗?感谢。
答案 0 :(得分:0)
这是一个几乎完整的解决方案:
DECLARE
@startat INT ,
@howmany INT
SET @startat = 5
SET @howmany = 10;
WITH table1 AS
(
SELECT ROW_NUMBER() OVER(ORDER BY DateTime DESC) AS RowNumber,
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)
)
SELECT *
FROM table1
WHERE RowNumber >= @startat
AND RowNumber < @startat + @howmany;
唯一缺少的是不同的方面,因为这会导致row_number出现问题。
你可以做的就是将select语句移动到一个函数中,该函数将执行不同的选择,然后在with语句中从中选择。
CREATE FUNCTION dbo.GetRecords(@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
然后with语句中的select类似于:
SELECT ROW_NUMBER() OVER(ORDER BY DateTime DESC) AS RowNumber,
l.*
FROM dbo.GetRecords(@UserName)