liveBook · Manning

End

As explained in Learn SQL in a Month of Lunches

END is a procedural SQL keyword that terminates a compound statement or stored-procedure body.

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

How Learn SQL in a Month of Lunches covers it

How deep it goes
Explains END through complete CASE and stored-procedure examples, without descending into database-engine internals.
Examples
Demonstrates price classification and an AddPromotion procedure that conditionally inserts promotion data.
What you can do after
Apply END correctly when writing CASE expressions and compound stored-procedure logic.
How it connects
Connects END with CASE expressions, IF statements, ELSE branches, BEGIN blocks, stored procedures, and INSERT statements.

This explainer was generated by Manning's knowledge graph from the text of Learn SQL in a Month of Lunches. The section links point to the parts of the book it draws on.