1. 程式人生 > >if語句&while語句

if語句&while語句

--if語句

declare @age int

select @age=DATEDIFF(year,stuBirthday,getdate()) from student where stuName='張飛'

if(@age>=18)

begin--begin...end必須有

   print N'已成年'

end

else

begin

print N'未成年'

end

--練習:計算全班同學英語平均分,如果是null,就當0分計

select AVG(math) from score

select * from score

declare @count

int

declare @sum int

select @count=COUNT(1) from score

select  @sum=SUM(math) from score

declare @average float

set @average=@sum/@count

select @average

--while 語句

--練習:計算1-100之間所有奇數的和

declare @sum int

set @sum=0

declare @i int

set @i=1

while @i<=100

begin

  if(@i%2!=0)

  begin

  set

@sum = @sum+@i

  end

  set @i=@i+1

end

select @i