END closes a SQL structure after its work is complete. It can finish a CASE expression, terminate an IF block as END IF, or close the BEGIN...END block that contains a stored procedure's statements. section 21.2.3 section 21.1.3
Use END in these closely related situations:
- Close a CASE expression after its WHEN and optional ELSE choices; assign an alias when it appears in SELECT. section 21.1.3
- Close an IF condition with END IF, then close the surrounding stored procedure with END. section 21.2.1
- Group several statements after ELSE by placing them inside BEGIN and END. section 21.2.3
Classifying prices with CASE
A query can test each title price and finish the decision with END: CASE Price WHEN 7.95 THEN 'This title is $7.95.' ELSE 'This title is not $7.95.' END AS IsPrice795. The same closing keyword works when CASE evaluates ROUND(Price, 0) or ranges such as Price < 8.00. section 21.1.3
SELECT
TitleName,
Price,
CASE Price
WHEN 7.95 THEN 'This title is $7.95.'
ELSE 'This title is not $7.95.'
END AS IsPrice795
FROM title;
Browse a complete SELECT query showing END closing a CASE expression and the resulting readable column alias. section 21.1.3
ELSE BEGIN
INSERT INTO promotion (
PromotionID,
PromotionCode,
PromotionStartDate,
PromotionEndDate
)
SELECT
_PromotionID,
_PromotionCode,
_PromotionStartDate,
_PromotionEndDate
;
SELECT 'INSERT successful.' AS Message;
END;
Browse a stored-procedure ELSE branch where BEGIN and END group an INSERT and a success message. section 21.2.3
