Control Structures  «Prev  Next»

PL/SQL Labels and GOTO Statement - Exercise

Create a PL/SQL block with labels and a GOTO statement

Objective: Practice identifying when labels and the GOTO statement can be used in Oracle PL/SQL, and compare that approach with a more structured alternative.

Exercise Scoring

This exercise is worth 25 points total:

  • 10 points for writing a working PL/SQL block that uses labels and GOTO.
  • 10 points for writing equivalent logic without labels and GOTO.
  • 5 points for explaining which style you prefer and why.

Background | Overview

A legacy PL/SQL codebase may still contain labels and GOTO statements, especially in older procedural logic. In modern Oracle development, structured control flow is usually preferred because it is easier to read, test, and maintain.

In this exercise, imagine that you are reviewing code for a pet store application. Your task is to write a PL/SQL block that locates the most recent sale in the CUSTOMER_SALE table and returns the FIRSTNAME of the related customer from the CUSTOMER table.

You will first solve the problem using labels and GOTO, then rewrite the logic using structured PL/SQL so you can compare both approaches.

Instructions

  1. Use the SALES_DATE column in the CUSTOMER_SALE table to identify the most recent sale.
  2. Use the related customer record in the CUSTOMER table to retrieve the FIRSTNAME value.
  3. Write one PL/SQL block that uses labels and the GOTO statement.
  4. Write a second PL/SQL block that solves the same problem without labels and without GOTO.
  5. Briefly explain which version you prefer and why.

Development Notes

In current Oracle PL/SQL practice, GOTO is valid syntax but is generally reserved for exceptional cases. Prefer structured constructs such as IF, CASE, LOOP, WHILE, and exception handling when they express the logic more clearly.

Download Files

Download the PETSTORE schema files and review the table definitions before writing your solution.

Submitting Your Exercise

Paste both PL/SQL solutions into the text area below. Include a short explanation stating which approach you prefer and why. When you are ready, click Submit.