gpt4 book ai didi

sql - T-SQL 跳过获取存储过程

转载 作者:行者123 更新时间:2023-12-02 00:33:14 25 4
gpt4 key购买 nike

我在这个网站上似乎运气不太好,但我仍然是个乐观主义者,我会继续努力。我有两个表,Journals 和 ArticleCategories,它们使用以下查询连接:

SELECT Journals.JournalId,
Journals.Year,
Journals.Title,
ArticleCategories.ItemText
FROM Journals
LEFT OUTER JOIN ArticleCategories
ON Journals.ArticleCategoryId = ArticleCategories.ArticleCategoryId

谁能告诉我如何将其重写为 Skip, Take 查询。换句话说,我希望它跳过前 n 条记录,然后获取下 n 条记录。我认为 ROW_NUMBER 涉及到某个地方,但我不知道如何在这种情况下使用它。

我怀疑运气不好的原因是我发现很难解释我想要做什么。如果我的问题不清楚,请随时告诉我哪里出错了,我很乐意再试一次。也许我还应该提到我正在尝试将其放入存储过程中。非常感谢。非常感谢,

最佳答案

2005/2008/2008 R2

;WITH cte AS
(
SELECT Journals.JournalId,
Journals.Year,
Journals.Title,
ArticleCategories.ItemText,
ROW_NUMBER() OVER
(ORDER BY Journals.JournalId,ArticleCategories.ItemText) AS RN
FROM Journals LEFT OUTER JOIN
ArticleCategories
ON Journals.ArticleCategoryId = ArticleCategories.ArticleCategoryId
)
SELECT JournalId,
Year,
Title,
ItemText
FROM cte
WHERE RN BETWEEN 11 AND 20

对于 2012 年来说,这更简单

SELECT Journals.JournalId,
Journals.Year,
Journals.Title,
ArticleCategories.ItemText
FROM Journals
LEFT OUTER JOIN ArticleCategories
ON Journals.ArticleCategoryId = ArticleCategories.ArticleCategoryId
ORDER BY Journals.JournalId,
ArticleCategories.ItemText
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY

关于sql - T-SQL 跳过获取存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5620758/

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