Created
August 30, 2011 05:51
-
-
Save hamstar/1180273 to your computer and use it in GitHub Desktop.
UNTESTED simple Oracle SQL procedure that gets a products price, multiplies it by the quantity and adds it to the sales table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Add a sale */ | |
CREATE OR REPLACE PROCEDURE add_sale( | |
v_cust_id customers.id%TYPE, | |
v_prod_id products.id%TYPE, | |
v_qty sales.qty%TYPE | |
) | |
IS | |
v_price_single products.price%TYPE; | |
v_price_total sales.price%TYPE; | |
BEGIN | |
/* get the price for the product */ | |
SELECT | |
price INTO v_price_single, | |
FROM products | |
WHERE id = v_prod_id; | |
IF SQL%NOTFOUND THEN RAISE e_prod_not_found; | |
/* calculate the sale price */ | |
v_price_total = :v_price_single * :v_qty; | |
/* insert the sales */ | |
INSERT INTO sales | |
( customer_id, product_id, price, quantity ) | |
VALUES( v_cust_id, v_prod_id, v_price_total, v_qty ); | |
/* check for exceptions */ | |
EXCEPTION | |
WHEN e_prod_not_found THEN | |
DBMS_OUTPUT.PUT_LINE('ERROR: Product not found'); | |
END add_sale; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment