简体   繁体   中英

update/insert PL/SQL

Let's say that Customer 4 wishes to increase their order from 23 to 100. Enable the user to type in:

  1. The number 4 for customer_ID
  2. 100 for the updated quantity.

I want to Write a PL/SQL function to receive these two values and update the sales table to reflect this change. Print out on screen from the main section of code the total quantity for customer 4 before and after the update. Please help I have tried with the following code but not sure about the structure

CREATE OR REPLACE TRIGGER orders_before_insert
BEFORE INSERT
 ON ord
 FOR EACH ROW

DECLARE
  v_price NUMBER;
 new number;
BEGIN

 SELECT pr INTO v_price FROM product where product_id =:new.product_id
 ;

-- Update create_date field to current system date
:new.total_cost := :new.quantity * v_price;

END;

Here is a proof of concept procedure. It doesn't have the validation or error handling we would expect in a proper procedure.

create or replace procedure update_order 
    (p_order_id in orders.id%type
      , p_additional_qty in orders.qty%type
      , p_orig_total out number 
      , p_new_total out number )
is
    l_total number;
    l_orig number;
begin
    update orders
    set qty = qty + p_additional_qty
    where id = p_order_id
    returning (qty - p_additional_qty)* price 
               ,  qty * price into l_orig, l_total;
    p_orig_total := l_orig;
    p_new_total := l_total;

end update_order;
/

In SQL*Plus we use the ACCEPT command to get input values from a user. We declare variables to hold computed values with VAR, and output them with PRINT.

Here is the test data:

SQL> select * from orders;

        ID        QTY      PRICE
---------- ---------- ----------
        42         23      19.99

SQL>

And here is how we call the procedure using the SQL*Plus EXECUTE command:

SQL> var tot number
SQL> var orig number
SQL> accept order_id prompt "enter order ID: "
enter order ID: 42
SQL> accept add_qty prompt "please enter add qty: "
please enter add qty: 77
SQL> exec update_order (&order_id, &add_qty, :orig, :tot)

PL/SQL procedure successfully completed.

SQL> print :orig

      ORIG
----------
    459.77

SQL> print :tot

       TOT
----------
      1999

SQL> 

To script it, just place all the commands in a text file, and run it in SQL*Plus like this:

SQL>  @your_script.sql

Note that I have deviated from your assignment's instructions in a couple of ways.

  1. I don't display the original total cost upfront. To do that you would need to select the record first then update it. In real life we would want to avoid two operations.
  2. I'm calculating the totals rather then storing them. Again this is the approach we would take in a real system. However, if your table actually has an TOTAL_COST column you would need to amend the UPDATE according.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM