User-Defined Functions «Prev 

Oracle 26ai PL/SQL Function Exercise: Calculate Sales Tax

Objective: Create a stored function that returns a numeric tax rate for a Pet Store customer, then use that result to update the tax on one sale. This exercise applies the numeric-return concepts from Lesson 4.

Background and Course Rules

The Pet Store uses a simplified rule for this exercise: a customer's home-state abbreviation selects one of four rates. These are fictional teaching rates, not current tax rates or guidance for a real checkout system. Keep them unchanged so that your answer can be compared with the model solution.

Fictional rates used only in this exercise
State abbreviationPercentageFunction result
CA4.5%0.045
HI4.03%0.0403
NE3.5%0.035
WI5.0%0.050

Calculate tax from the sum of SALE_ITEM.SALE_AMOUNT for the selected sale. Shipping and handling are excluded. For this practice task, treat null line amounts as zero and use a zero subtotal when there are no line items. Round the final tax amount to two decimal places.

Prerequisites and Download Files

Use the Pet Store setup instructions and SQL downloads in a dedicated Oracle 26ai practice schema. You need the CUSTOMER, CUSTOMER_SALE, and SALE_ITEM tables, with their sample data and keys. The CREATE PROCEDURE privilege permits creating a stored function in your own schema.

You may also complete the exercise as a written script review. State whether you executed your tests in Oracle or only reasoned through their expected results.

Instructions

  1. Create GET_PERCENT(I_CUST_ID IN NUMBER) RETURN NUMBER. Look up the matching customer's STATE using CUSTOMER.CUST_ID. Normalize the abbreviation with UPPER(TRIM(...)) and return the corresponding decimal rate.
  2. Raise an application error for a null customer ID (-20011), a customer that does not exist (-20010), or a missing/unsupported state (-20020). Do not silently return zero for invalid customer data. Allow unexpected database errors to propagate.
  3. Write an UPDATE that sets only CUSTOMER_SALE.TAX_AMOUNT for SALES_ID = 34. Correlate the subtotal to that sale, call GET_PERCENT with its customer ID, and apply ROUND(..., 2). Do not change other sales.
  4. Include a query that previews the customer, state, subtotal, and rate, and another that verifies the updated tax. On fresh course data, expect customer 26, state CA, a subtotal of 38.99, and tax of 1.75.
  5. Document the four rates, error codes, null policy, and tests. Include a normal case, each supported state, and the three invalid-input cases. If fixtures are needed, describe or isolate them in the practice schema.

This task updates the tax column only. Recalculating TOTAL_SALE_AMOUNT is outside its scope, so do not interpret this isolated update as a complete order-recalculation routine.

Hints and Practice Transaction

  • Place local declarations between AS and BEGIN; an extra nested DECLARE block is unnecessary. Keep precision/scale out of RETURN NUMBER.
  • Use the table alias in C.CUST_ID = I_CUST_ID to make the column and parameter distinct. The primary key makes the customer lookup a single-row lookup.
  • NVL(SUM(...), 0) provides the chosen zero-subtotal policy. The subquery's sales-ID condition does not replace the outer UPDATE's WHERE CS.SALES_ID = 34.
  • Compile the function and inspect its errors before starting the practice update. DDL such as CREATE OR REPLACE FUNCTION commits implicitly, so it must precede your test savepoint.
  • With client autocommit disabled, create a savepoint, update sale 34, inspect the result, and roll back to the savepoint. Do not issue DDL or COMMIT between that savepoint and the rollback. The rollback undoes test DML, not function creation.

Exercise Scoring

This is a 25-point self-assessment. The result page displays your response and a model solution for comparison. It does not execute your SQL, automatically grade your script, or award recorded credit.

25-point self-assessment rubric
CriterionPoints
Valid function heading, declarations, executable body, and return values5
Correct decimal rates; correlated item subtotal; rounding; UPDATE restricted to sale 348
Explicit input/state errors; useful exception handling; normal and boundary test cases7
Clear comments and documentation of parameters, assumptions, errors, and expected results5

Submit Your Exercise

Submit the complete function, UPDATE, verification queries, and brief documentation together. Identify the script filename, author, date, input parameter, returned value, assumptions, handled errors, and test outcomes.


Namecheap 1