T-SQL流程控制语句
练习:用While循环求1~100的偶数和。
Declare @number smallint,@sum smallint
Set @number=1
Set @sum=0
While @number<=100
Begin
If @number%2=0
Begin
Set @sum=@sum+@number
End
Set @number=@number+ 1
End
Print '1到100之间偶数的和为'+str(@sum)
Go
练习:计算1~100之间所有能被3整除的数的个数和总和。
Declare @number smallint,@sum smallint,@countsmallint
Set @number=1
Set @sum=0
Set @count=0
While @number<=100
Begin
If @number%3=0
Begin
Set @count=@count+1
Set @sum=@sum+@number
End
Set @number=@number+ 1
End
Print '1到100之间能被3整除的数的个数为'+str(@count)
Print '1到100之间能被3整除的数的总和为'+str(@sum)
GO