Skip to content

Instantly share code, notes, and snippets.

@hamstar
Created August 30, 2011 05:51
Show Gist options
  • Save hamstar/1180273 to your computer and use it in GitHub Desktop.
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
/* 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