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

2024-12-20 16:08:50
推荐回答(1个)
回答1:

create proc test2
as
begin
declare @i int
declare @m int
declare @n int
declare @table1 table(id int identity,num int)
set @i=1
set @n=1
set @m=0
while @i<=100
begin
declare @j int
INSERT INTO @table1(num)
SELECT CAST(RAND()*100 AS INT)+1
set @i=@i+1
end
while @n<=100
begin
select @m=@m+num from @table1 where id=@n
set @n=@n+1
end
print @m
end

--运行:
test2