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.
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.
| State abbreviation | Percentage | Function result |
|---|---|---|
| CA | 4.5% | 0.045 |
| HI | 4.03% | 0.0403 |
| NE | 3.5% | 0.035 |
| WI | 5.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.
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.
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.-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.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.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.
AS and BEGIN; an extra nested DECLARE block is unnecessary. Keep precision/scale out of RETURN NUMBER.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.CREATE OR REPLACE FUNCTION commits implicitly, so it must precede your test savepoint.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.
| Criterion | Points |
|---|---|
| Valid function heading, declarations, executable body, and return values | 5 |
| Correct decimal rates; correlated item subtotal; rounding; UPDATE restricted to sale 34 | 8 |
| Explicit input/state errors; useful exception handling; normal and boundary test cases | 7 |
| Clear comments and documentation of parameters, assumptions, errors, and expected results | 5 |
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.