laika222
5/10/2017 - 6:46 PM

IF, IF ELSE, ELSEIF Statment MS.mysql

// In SQL Server, an IF ELSE statement can be done inside a stored procedure, or outside a stored procedure. If you are doing one logical test, you will use an IF statement to see if the logical test is true, and an ELSE statement to tell it what to do if the logical statement is false. If you are using two or more logical tests, you will use an IF statement, an ELSE IF statement (two words) to complete a second logical test if the first (the IF) is false (you can have as many ELSE IF statements as you want in order to complete more than two logical tests), and an ELSE statement to tell it what to do if all of the IF/ELSE IF logical tests are false. 

// EXAMPLE outside of a stored procedure

IF (SELECT Product FROM products1 WHERE ProductID = 1) = 'Guitar'
	SELECT 'This is a guitar' AS Result;
ELSE IF (SELECT Product FROM products1 WHERE ProductID = 1) = 'Keyboard'
	SELECT 'This is a keyboard' AS Result;
ELSE SELECT 'This is neither a guitar nor a keyboard' AS Result;


// EXAMPLE inside a stored procedure

CREATE PROCEDURE lp_product_identifier
@p_ProductID INT = NULL

AS
IF (SELECT Product FROM products1 WHERE ProductID = @p_ProductID) = 'Guitar'
	SELECT 'This is a guitar' AS Result;
ELSE IF (SELECT Product FROM products1 WHERE ProductID = @p_ProductID) = 'Keyboard'
	SELECT 'This is a keyboard' AS Result;
ELSE SELECT 'This is neither a guitar nor a keyboard' AS Result;
GO


EXEC lp_product_identifier
@p_ProductID = 2


// DETAIL!!! EXAMPLE outside of a stored procedure

// Logical test 1, IF Product field WHERE ProductID = 1 equals 'Guitar',
IF (SELECT Product FROM products1 WHERE ProductID = 1) = 'Guitar'

// SELECT 'This is a guitar'
	SELECT 'This is a guitar' AS Result;
	
// Logical test 2, ELSE IF (two words, used when adding a second IF statement) Product field WHERE ProductID = 1 equals 'Keyboard',
ELSE IF (SELECT Product FROM products1 WHERE ProductID = 1) = 'Keyboard'

// SELECT 'This is a keyboard'
	SELECT 'This is a keyboard' AS Result;
	
// ELSE (it chooses this option if the IF statement and the ELSE IF statement are both false) SELECT 'This is neither a guitar nor a keyboard'
ELSE SELECT 'This is neither a guitar nor a keyboard' AS Result;


// DETAIL!!! EXAMPLE inside a stored procedure

// Creates procedure lp_product_identifier
CREATE PROCEDURE lp_product_identifier

// Creates an input variable p_ProductID, an integer, with a default value of NULL
@p_ProductID INT = NULL

// AS starts the core of the procedure
AS

// Begin IF statement - logical test 1, IF Product field for @p_ProductID entered by user equals 'Guitar',
IF (SELECT Product FROM products1 WHERE ProductID = @p_ProductID) = 'Guitar'

// SELECT 'This is a guitar'
	SELECT 'This is a guitar' AS Result;
	
// Logical test 2, ELSE IF Product field for @p_ProductID entered by user equals 'Keyboard',
ELSE IF (SELECT Product FROM products1 WHERE ProductID = @p_ProductID) = 'Keyboard'

// SELECT 'This is a keyboard'
	SELECT 'This is a keyboard' AS Result;
	
// ELSE (it chooses this option if the IF statement and the ELSE IF statement are both false) SELECT 'This is neither a guitar nor a keyboard'
ELSE SELECT 'This is neither a guitar nor a keyboard' AS Result;

// GO runs the code up to the AS
GO

// Executes procedure lp_product_identifier, entering a value of 2 for p_ProductID
EXEC lp_product_identifier
@p_ProductID = 2
// In MySQL, you must place an IF ELSE statement within a stored procedure. If you are doing one logical test, you will use an IF statement to see if the logical test is true, and an ELSE statement to tell it what to do if the logical statement is false. If you are using two or more logical tests, you will use an IF statement, an ELSEIF statement (one word) to complete a second logical test if the first (the IF) is false (you can have as many ELSEIF statements as you want in order to complete more than two logical tests), and an ELSE statement to tell it what to do if all of the IF/ELSEIF logical tests are false. MySQL uses the THEN keyword to tell it what to do if one of the logical tests is true.

DELIMITER $

DROP PROCEDURE IF EXISTS product_test $

CREATE PROCEDURE product_test(p_ProductID INT)

BEGIN

DECLARE product_entry INT;
SET product_entry = p_ProductID;

IF (SELECT Product FROM products1 WHERE ProductID = product_entry) = 'Guitar'
  THEN SELECT 'This is a guitar' AS Result;
ELSEIF (SELECT Product FROM products1 WHERE ProductID = product_entry) = 'Keyboard'
  THEN SELECT 'This is a keyboard' AS Result;
ELSE SELECT 'This is neither a guitar nor a keyboard' AS Result;
END IF;
    
END $

DELIMITER ;

call product_test(2);


// DETAIL!!!

// Set delimiter to $ so you can set up statements within the procedure
DELIMITER $

// Drop procedure product_test if it exists, allowing you to recreate the procedure while editing
DROP PROCEDURE IF EXISTS product_test $

// Create procedure product_test, with one parameter, p_ProductID (as integer) where the user can input the ProductID being tested
CREATE PROCEDURE product_test(p_ProductID INT)

// Begin the procedure
BEGIN

// Declare variable product_entry as integer
DECLARE product_entry INT;

// Set variable product_entry as equal to whatever the user plugged in for p_ProductID
SET product_entry = p_ProductID;

// Begin IF statement - logical test 1, IF Product field for product_entry equals 'Guitar',
IF (SELECT Product FROM products1 WHERE ProductID = product_entry) = 'Guitar'

// THEN SELECT 'This is a guitar'
	THEN SELECT 'This is a guitar' AS Result;
	
// Logical test 2 - ELSEIF (one word, used when adding a second IF statement) Product field for product_entry equals 'Guitar', 
ELSEIF (SELECT Product FROM products1 WHERE ProductID = product_entry) = 'Keyboard'
    
// THEN SELECT 'This is keyboard'
  THEN SELECT 'This is a keyboard' AS Result;
        
// ELSE (it chooses this option if the IF statement and the ELSEIF statement are both false) SELECT 'This is neither a guitar nor a keyboard'
ELSE SELECT 'This is neither a guitar nor a keyboard' AS Result;
    
// END the IF statement
END IF;

// END the stored procedure
END $

// Set the delimiter back to ;
DELIMITER ;

// CALL the procedure product_test, plugging in 2 for the value of parameter p_ProductID
CALL product_test(2);