日期:2014-05-18 浏览次数:20638 次
CREATE TABLE #TEST(SHUZI INT) declare @A int, @B INT SET @A=1 SET @B=100 PRINT @A WHILE @A<=@B BEGIN INSERT INTO #TEST VALUES(@A) SET @A=@A+1 END SELECT * FROM #TEST SELECT sum(SHUZI) FROM #TEST
declare @A int, @B INT, @ssum int SET @A=1 SET @B=100 set @ssum=0 PRINT @A WHILE @A<=@B BEGIN set @ssum=@ssum+@a SET @A=@A+1 END print @ssum
------解决方案--------------------
DECLARE @A INT DECLARE @Total INT SET @A = 1 SET @Total = 0 WHILE @A < 101 BEGIN SET @Total = @Total + @A SET @A = @A + 1 END SELECT @Total
------解决方案--------------------
declare @A int, @B INT, @Sum int SET @A=1 SET @B=100 set @sum=0 PRINT @A WHILE @A<=@B BEGIN SET @A=@A+1 SET @Sum=@Sum+@A END SELECT @Sum
------解决方案--------------------
[code=SQL]
--CREATE TABLE #TEST(SHUZI INT)
--
--declare @A int,
-- @B INT
--SET @A=1
--SET @B=100
--PRINT @A
--WHILE @A <=@B
-- BEGIN
--INSERT INTO #TEST VALUES(@A)
--SET @A=@A+1
--END
--
--SELECT * FROM #TEST
--SELECT sum(SHUZI) FROM #TEST
declare @B INT
SET @B=100
;WITH c AS(
SELECT 1 AS Row
UNION ALL
SELECT Row+1 AS Row FROM c
WHERE c.Row <@B
)
SELECT SUM(Row) FROM c
[/code]