How to use several "IF" Conditionals in a Store Procedure in SQL Server

3

I would like to know how I can use several IFs in a Store procedure in SQL Server, How would Syntax be? Here I have an example of one, but this has only one If and one Else and within that ELSE there are two others, but I need to do more and it does not work ...

DECLARE @Number int;  
SET @Number = 50;  
IF @Number > 100  
   PRINT 'The number is large.'  
ELSE   
  BEGIN  
  IF @Number < 10  
  PRINT 'The number is small.' 
ELSE  
   PRINT 'The number is medium.' 
END  
    
asked by keiver vasquez 16.11.2018 в 17:36
source

2 answers

1

To accomplish what you need, you only have to enclose in BEGIN / END blocks each condition that you need to build in the following way:

IF(Condición)
 BEGIN
  /*Código*/
 END
ELSE 
   IF(Condición)
      BEGIN
         /*Código*/ 
      END
   ELSE
      BEGIN
         /*Código*/
      END

I leave the documentation so you can review it

    
answered by 16.11.2018 / 17:47
source
0
IF (La condicion)
begin

(Si hay mas de una orden dentro del if escribir begin ... end)

end

Else if (segunda condicion)

begin 

end

and when you finish with the if

ELSE 

   BEGIN

   END
    
answered by 27.11.2018 в 19:28