How to use the procedure IF statement

MSSQL procedure IF statement is a reserved word used to process branches according to conditions. Other programming languages ​​may have differences in grammar, but the definition is the same.

Procedure IF statement definition and explanation.

  • The IF statement is the most basic concept in programming languages.
  • The IF statement is used to process branches in procedures.
  • IF statements can be used nested in procedures.
  • Procedure IF statements can use multiple conditional statements.

Procedure IF statement flow chart.

How to use the IF statement in the procedure.

How to use the IF branch processing.

  • The procedure IF statement needs to have a begin and end block for better maintenance.
-- How to use the IF branch processing.
-- DROP PROCEDURE [STORE_PROCEDURE_IF_STATEMENT_EX1];
CREATE  PROCEDURE [DBO].[STORE_PROCEDURE_IF_STATEMENT_EX1]        
    @UseAge INT
AS
BEGIN
   IF(@UseAge = 20)   
   BEGIN
      PRINT('Print if UseAge is 20.');
   END
   ELSE IF (@UseAge = 30) 
   BEGIN
      PRINT('Print if UseAge is 30.');
   END
   ELSE 
   BEGIN 
     PRINT('Print if UseAge is not 20 or 30.');
   END
END;

How to use nested IF statements in procedures.

  • You can use nested IF statements inside IF conditional statements.
-- How to use nested IF statements in procedures.
-- DROP PROCEDURE [[STORE_PROCEDURE_IF_STATEMENT_EX2]];
CREATE  PROCEDURE [DBO].[STORE_PROCEDURE_IF_STATEMENT_EX2]  
    @UserNm  VARCHAR(100),
    @UseAge INT
AS
BEGIN  
  IF(@UseAge = 20) 
  BEGIN
    IF(@UserNm ='Lee')
    BEGIN
     PRINT('Print if UseAge: 20, UserNm:Lee. ');
    END
    ELSE
    BEGIN
      PRINT('If UseAge: 20 and UserNm: not Lee, print');
    END 
  END
  ELSE
   BEGIN
     PRINT('If UseAge is not 20, print it.');
   END 
END;

Procedure IF statement uses multiple conditional statements.

  • You can write multiple conditional statements using AND and OR reserved words in the IF conditional statement.
-- Procedure IF statement uses multiple conditional statements.
-- DROP PROCEDURE [STORE_PROCEDURE_IF_STATEMENT_EX3];
CREATE  PROCEDURE [DBO].[STORE_PROCEDURE_IF_STATEMENT_EX3]  
   @UserNm  VARCHAR(100),
   @UseAge INT,
   @UserAddress  VARCHAR(100)
AS
BEGIN
   IF(@UseAge IN('10','20','30') AND  @UserNm = 'Lee' and @UserAddress = 'LA' ) 
   BEGIN
     PRINT('If UseAge: 10, 20, 30 are included, UserNm: Lee, and UserAddress: LA, print it. ');
   END
   ELSE
   BEGIN
     PRINT('If UseAge: 10, 20, 30 is included, UserNm: Lee, and UserAddress: is not LA, print it. ');
   END
END;

Ending the procedure IF statement.

  • The case when statement can also be used for branching with the if statement.
  • In T-SQL, you can use the IF statement by using the declare statement.
  • In MSSQL, you can also use conditions such as between, in, and not.

Categories:

Updated:

Leave a comment