首页 > 代码库 > 13、SQL基础整理(流程控制begin……end)

13、SQL基础整理(流程控制begin……end)

流程控制

begin ……end将一个语句块包含起来,中间可以写任何语句

格式:

begin--开始

    select *from student

end--结束

 

if

declare @bianliang int

set @bianliang = 10

if @bianliang>10 print ‘你好‘

else print ‘hello‘

 

if后面有多个语句,用begin  end作为分隔点

declare @shuxue decimal(18,2)

select @shuxue = MAX(shuxue) from fenshu

declare @stuid int

select @stuid = code from fenshu where shuxue = @shuxue

---select *from student where code = @stuid

declare @sex varchar(20)

select @sex = sex from student where code = @stuid

if @sex = ‘男‘

print‘这是男同学‘

else

print‘这是女同学‘

 

 

while

declare @shuxue int

set @shuxue = 75

while @shuxue<80

begin

    print‘很好‘

    set @shuxue = @shuxue+1

    if @shuxue = 79

    break

    print ‘ok‘

end

 

declare @shuxue int

set @shuxue = 80

while @shuxue<95

begin

    print‘很好‘+cast(@shuxue as varchar(10))

    set @shuxue = @shuxue+1

    if @shuxue = 93

      break

    if @shuxue>85 and @shuxue<90

      begin

        continue

      end

    print ‘ok‘

end

 

13、SQL基础整理(流程控制begin……end)