Multiple condition in single IF statement

sql, sql-server, sql-server-2008

Solution

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 

Problem

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?

Original source