如果然后在SQL中声明

时间:2017-02-18 08:00:14

标签: sql sql-server

为什么if then语句不能在这里工作

declare @num integer(2);
set @num = 10;

begin
    if (@num < 10)
    Then
        print('hello');

    set @num = @num +1 ;

    else
        print('over');

    set @num = @num +1 ;
    END IF;
END;

我收到这些错误:

  

第15行,第15行,第1行,第9行   关键字&#39;然后&#39;附近的语法不正确。

     

Msg 102,Level 15,State 1,Line 12
  关键字&#39; else&#39;附近的语法不正确。

2 个答案:

答案 0 :(得分:3)

试试这个:

 declare @num int;
 set @num = 10;

 if (@num < 10)
 begin
   print('hello');
   set @num = @num +1;
 end
 else 
    begin
      print('over');
      set @num = @num +1;
    end

答案 1 :(得分:1)

How many teams do you have to enter?  2
Team 1 City:  Boston
Team 1 Name:  Bruins
Team 2 City:  Toronto
Team 2 Name:  Maple Leafs
City: Boston | Team: Bruins
City: Toronto | Team: Maple Leafs
The last team entered was: Toronto Maple Leafs