If else in stored procedure sql server
IF ELSE in stored procedure:
IF ELSE condtion used for chechking condtion.If condtion true then if staement print,if condtion false then else stament print,
Syntax:
If (condtion)
{ sql staement }
else
{ Sql staement }
Example:
CREATE PROCEDURE grade
(
@MaxAge INT --PARAMETER
)
AS
BEGIN
IF(@MaxAge>=18) --CONDITION
BEGIN
PRINT 'YOU ARE ELIGIBLE FOR ADMITION'--CONDITION TRUE
END
ELSE
BEGIN
PRINT 'SORRY, YOU ARE NOT ELIGIBLE FOR ADMITION'--CONDITION FALSE
END
END
Execute Procedure with parameter and see output
-- EXECUTE PROCEDURE
EXEC ELIGIBILITY 2
Output- SORRY, YOU ARE NOT ELIGIBLE FOR ADMITION
EXEC ELIGIBILITY 22
Output- YOU ARE ELIGIBLE FOR ADMITION
EXEC ELIGIBILITY 2
Output- SORRY, YOU ARE NOT ELIGIBLE FOR ADMITION
EXEC ELIGIBILITY 22
Output- YOU ARE ELIGIBLE FOR ADMITION