至于为什么
这将每次都返回零行数
您已经拥有最佳排名,因此不会有categoryid> @catID
@CatID不分配它不是根本
SELECT TOP(1)
@catID = categoryid
FROM
[Production].[Categories]
WHERE
categoryid > @catID
ORDER BY
categoryid
修复
DECLARE @catID int
DECLARE @count int = 1
SELECT TOP(1)
@catID = categoryid
FROM
[Production].[Categories]
ORDER BY
categoryid
WHILE @count > 0
BEGIN
PRINT @catID --do something
SELECT TOP(1)
@catID = categoryid
FROM
[Production].[Categories]
WHERE
categoryid > @catID
ORDER BY
categoryid
Select @count = select count(*) FROM [Production].[Categories]
WHERE categoryid > @catID
END
赋值为null
的值,但是你的逻辑有更多的问题。在获得前1名后,你拥有它。没有更多的前1名。
或
DECLARE @catID int
DECLARE @catIDnew int
SELECT TOP(1)
@catID = categoryid
FROM
[Production].[Categories]
ORDER BY
categoryid
WHILE @catID IS NOT NULL
BEGIN
PRINT @catID --do something
@catIDnew = null
SELECT TOP(1)
@catIDnew = categoryid
FROM
[Production].[Categories]
WHERE
categoryid > @catID
ORDER BY
categoryid
@catID [email protected]
END
Hy thanx为您的答案。但是它的错误,我认为是因为WHERE在SELECT之前执行,所以categoryis> NULL永远不会是真的。 – Breschi