-1

Why the if then statement is not working here

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;
     

I get these errors:

Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'Then'.

Msg 102, Level 15, State 1, Line 12
Incorrect syntax near the keyword 'else'.

Community
  • 1
  • 1
Basit
  • 862
  • 1
  • 12
  • 30
  • remove then from your query – Camahalan Royette Feb 18 '17 at 08:01
  • https://stackoverflow.com/questions/15085990/can-you-have-if-then-else-logic-in-sql/15086050#15086050 – Alex Feb 18 '17 at 08:08
  • Possible duplicate of [Can you have if-then-else logic in SQL?](http://stackoverflow.com/questions/15085990/can-you-have-if-then-else-logic-in-sql) – Alex Feb 18 '17 at 08:08
  • If you have **more than one statement** after your `IF` or `ELSE`, you must wrap it in `BEGIN .... END` delimiters! And there's no `THEN` in T-SQL .... – marc_s Feb 18 '17 at 08:09
  • Possible duplicate of [How to perform an IF...THEN in an SQL SELECT?](http://stackoverflow.com/questions/63447/how-to-perform-an-if-then-in-an-sql-select) – nhouser9 Feb 18 '17 at 08:31

2 Answers2

4

Try this :

 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
Ilyes
  • 14,640
  • 4
  • 29
  • 55
2
 declare @num integer
 set @num = 10

 if (@num < 10)
 begin
   print('hello')
   set @num = @num +1
 end
 else begin
   print('over')
   set @num = @num +1
 end
juergen d
  • 201,996
  • 37
  • 293
  • 362