// 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