参考以下代码
DECLARE @TB TABLE (TSUM INT)
DECLARE @NEXT1 INT
DECLARE @NEXT2 INT
SET @NEXT1=1
SET @NEXT2=1
WHILE @NEXT1<=16
BEGIN
IF @NEXT1*@NEXT2=16
BEGIN
PRINT @NEXT1
PRINT @NEXT2
INSERT INTO @TB VALUES(@NEXT1)
INSERT INTO @TB VALUES(@NEXT2)
BREAK
END
ELSE
BEGIN
WHILE @NEXT2<=16
BEGIN
IF @NEXT1*@NEXT2=16
BEGIN
PRINT @NEXT1
PRINT @NEXT2
INSERT INTO @TB VALUES(@NEXT1)
INSERT INTO @TB VALUES(@NEXT2)
BREAK
END
SET @NEXT2=@NEXT2+1
END
SET @NEXT1=@NEXT1+1
SET @NEXT2=1
END
END
SELECT DISTINCT TSUM AS 所有因子 FROM @TB WHERE TSUM<>16
SELECT SUM( DISTINCT TSUM) AS 求和 FROM @TB WHERE TSUM<>16
所有因子
-----------
1
2
4
8
(4 row(s) affected)
求和
-----------
15
(1 row(s) affected)