至於爲什麼
這將每次都返回零行數
您已經擁有最佳排名,因此不會有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