Multiple condition in single IF statement

122,776

Yes that is valid syntax but it may well not do what you want.

Execution will continue after your RAISERROR except if you add a RETURN. So you will need to add a block with BEGIN ... END to hold the two statements.

Also I'm not sure why you plumped for severity 15. That usually indicates a syntax error.

Finally I'd simplify the conditions using IN

CREATE PROCEDURE [dbo].[AddApplicationUser] (@TenantId BIGINT,
                                            @UserType TINYINT,
                                            @UserName NVARCHAR(100),
                                            @Password NVARCHAR(100))
AS
  BEGIN
      IF ( @TenantId IS NULL
           AND @UserType IN ( 0, 1 ) )
        BEGIN
            RAISERROR('The value for @TenantID should not be null',15,1);

            RETURN;
        END
  END 
Share:
122,776
Billa
Author by

Billa

Updated on July 09, 2022

Comments

  • Billa
    Billa almost 2 years

    I want to add multiple condition in single IF statement in SQL.

    I am not good in SQL & referred some example, all are showing only one condition in IF.

    Here is my procedure.

    CREATE PROCEDURE [dbo].[AddApplicationUser]    
    (  
    @TenantId BIGINT,  
    @UserType TINYINT,
    @UserName  NVARCHAR(100),  
    @Password NVARCHAR(100)  
    )  
    
    AS    
    
    BEGIN   
    
    IF ((@TenantId IS NULL)  AND (@UserType=0 OR @UserType=1) )
      RAISERROR('The value for @TenantID should not be null', 15, 1) -- with log 
    

    Is this correct syntax in SQL for multiconditions in IF?

  • Billa
    Billa about 11 years
    Your direct link also says only one condition example IF cost <= compareprice. do i need to go with nested loops ?
  • www
    www about 11 years
    For exemple (cost <= compareprice AND cost <= 2) is also a valid boolean expresion.