用T-SQL脚本程序编写程序,随机产生100个1到100之间的自然数,计算其累加和并输出。

2024-12-16 14:12:13
推荐回答(2个)
回答1:

DECLARE @Count int
DECLARE @Sum int
DECLARE @Num int

SELECT @Count=100,@Sum=0

WHILE (@Count>0)
BEGIN
SELECT @Num=CAST(RAND()*100 AS INT)
SELECT @Sum=@Sum+@Num,@Count=@Count-1
END

PRINT CONVERT(NVARCHAR(10),@Sum)

回答2:

DECLARE @Count int
DECLARE @Sum int
DECLARE @Num int

SELECT @Count=100,@Sum=0

WHILE (@Count>0)
BEGIN
SELECT @Num=CAST(RAND()*100 AS INT)
SELECT @Sum=@Sum+@Num,@Count=@Count-1
END

PRINT CONVERT(NVARCHAR(10),@Sum) 才