更新游标中的多行

时间:2019-02-15 17:20:46

标签: database oracle plsql oracle11g plsqldeveloper

我正在尝试逐步开发此程序。像下面的代码一样,我想在使用光标更新表库存后打印旧价格并打印新价格。

使用游标更新许多数据行,创建一个接受数字表示价格上涨百分比的过程。该过程将显示旧价格,新价格,并使用新价格更新数据库。

CREATE OR REPLACE PROCEDURE p5 
AS
CURSOR invent_cur IS
SELECT inv_price
FROM inventory;
v_inv_price inventory.inv_price%TYPE;
BEGIN
OPEN invent_cur;
LOOP
FETCH invent_cur INTO v_inv_price;
EXIT WHEN invent_cur%NOTFOUND;

IF (v_inv_price <= 0)
THEN
UPDATE inventory
set inv_price = inv_price*10;
DBMS_OUTPUT.PUT_LINE(v_inv_price);
END IF;

END LOOP;
CLOSE invent_cur;
END;
/

1 个答案:

答案 0 :(得分:0)

只需在更新语句周围尝试一下:

console.log("Moment (now):", moment(Date.now()));
console.log("Moment:", moment());

要执行:

CREATE OR REPLACE PROCEDURE p5 (in_pc IN number)
AS
CURSOR invent_cur IS
SELECT inv_price
FROM inventory
WHERE inv_price > 0 
FOR UPDATE;
v_inv_price inventory.inv_price%TYPE;
BEGIN
OPEN invent_cur;
LOOP
FETCH invent_cur INTO v_inv_price;
EXIT WHEN invent_cur%NOTFOUND;

DBMS_OUTPUT.PUT_LINE('before: '||v_inv_price);
v_inv_price := v_inv_price * in_pc ;

UPDATE inventory
set inv_price = v_inv_price
WHERE CURRENT OF invent_cur;

DBMS_OUTPUT.PUT_LINE('after: '||v_inv_price);

END LOOP;