我想用pl/sql函數更新下面的表格,事情是我設法寫了一個觸發器代碼,但我想用'function'來代替它。 我想爲顧客5從30增加其以200和使用戶能夠輸入:更新表格pl/sql
1)CUSTOMER_ID和 2)200爲更新的量的數字5。 並在更新之前和之後打印出客戶5的總數量。
Create table sales (customer_ID number(10), product_ID number(10), quantity number(10));
INSERT INTO sales (customer_ID, product_ID, quantity) Values(3,1,23);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(1,2,34);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(1,3,654);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(3,7,32);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(4,3,23);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(3,3,111);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(5,4,6);
觸發代碼中,我寫道:
create or replace trigger quantity_change
before insert or update of quantity on sales
for each row
WHEN (NEW.customer_id > 0)
DECLARE
qua number;
BEGIN
qua := :NEW.quantity - :OLD.quantity;
dbms_output.put_line('Old quangtity: ' || :OLD.quantity);
dbms_output.put_line('New quantity: ' || :NEW.quantity);
dbms_output.put_line('diiference quangtity: ' || qua);
END;
UPDATE sales
SET quantity = 200 WHERE customer_id = 5;
我設法寫這個程序,但仍然堅持,不知道如何啓用使用
CREATE or replace PROCEDURE Updatesales
(
customer_ID number,
product_ID number,
quantity number)
AS
BEGIN
UPDATE sales
SET quantity= 100
WHERE customer_id= 4;
END;
我想用一個函數來解決這個問題,功能會像這樣:
CREATE [OR REPLACE] FUNCTION function_name [(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype {IS | AS} BEGIN <function_body> END [function_name];
請指教
爲什麼?你的要求背後的目的是什麼? – Boneist
更多功能知識 – Chaz
所以這是一個自學知識練習來了解功能?首先,如果我是你,我會嘗試寫這樣的功能。您必須決定您需要的輸入和輸出參數,然後考慮如何使用參數來查找,更新和返回相關值。嘗試一下,如果您遇到困難或有其他問題,請使用目前爲止的內容更新您的問題。然後,我們可以給你更有幫助的指針。 – Boneist