请问这一段语句是不是循环填充topN列?
临时表#TblHighRunner里有五列,其中ProductLine, PartNumber, PartDesc, Qty列都已填充,只有TopN列没被填充。这段代码是填充TopN列的,请问其循环机制是怎么样的?我看的很糊涂。
DECLARE @PL VARCHAR(25)
, @PN VARCHAR(25)
, @TopN INT
SET
@PL = 'DSAM' WHILE (@PL IS NOT NULL) BEGIN
SET
@PL =
(SELECT
TOP 1 ProductLine
FROM
#TblHighRunner
WHERE
TopN IS NULL
ORDER BY
ProductLine
, Qty DESC)
SET
@PN =
(SELECT
TOP 1 PartNumber
FROM
#TblHighRunner
WHERE
TopN IS NULL
ORDER BY
ProductLine
, Qty DESC)
UPDATE #TblHighRunner
SET
TopN = (CASE WHEN
(SELECT
MAX(TopN)
FROM
#TblHighRunner
WHERE
ProductLine = @PL) IS NULL
THEN 1
ELSE
(SELECT
MAX(TopN)
FROM
#TblHighRunner
WHERE
ProductLine = @PL) + 1
END)
WHERE
ProductLine = @PL
AND
PartNumber = @PN
SET
@TopN = (CASE WHEN
(SELECT
MAX(TopN)
FROM
#TblHighRunner