【发布时间】:2018-12-08 12:58:52
【问题描述】:
这是我第二次编写程序,这是我第一次使用游标。对于此过程,我不希望使用参数,但我遇到了问题。这是我目前拥有的:
Create or Replace Procedure Update_Balance as
Cursor C1 is
Select purchases.Cust_ID, Purchase_Amount, curr_balance,
credit_line,Pending_Flag
from Purchases
inner join customers on purchases.cust_id = customers.cust_id
where purchases.pending_flag = 1
for update of curr_balance;
PendingBalance purchases.purchase_amount%type;
PurchaseRow c1%RowType;
ProposedNewBalance purchases.purchase_amount%type;
Begin
Begin
Open C1;
Fetch c1 into PurchaseRow;
While c1% Found Loop
Select sum(Purchase_amount)
into PendingBalance
from purchases
where cust_id = c1.cust_id
and pending_flag = 1;
end;
ProposedNewBalance := PendingBalance + c1.curr_balance;
If ProposedNewBalance > C1.Credit_Line then
dbms_output.put_line('One or more purchases were not processed for Customer');
end if;
If ProposedNewBalance <= c1.Credit_Line then
update Customers
set curr_balance = ProposedNewBalance
where customer.cust_id = c1.cust_id;
end if;
If ProposedNewBalance <= c1.credit_line then
Update Purchases
set Pending_Flag = 1
where purchases.cust_id = c1.cust_id;
end if;
end;
【问题讨论】:
-
我使用的表是以下 CREATE TABLE CUSTOMERS ( cust_id char(6) 约束customers_pk PRIMARY KEY, first_name varchar2(100), last_name varchar2(100), credit_line number(10,2), curr_balance number(10,2), Earn_points number(5), tier_id char(2), 约束 tier_fk 外键 (tier_id) 引用 REWARDS_TIER, 约束 balance_chk 检查 (curr_balance
-
CREATE TABLE PURCHASES( purchase_id char(7) constraint purchase_pk PRIMARY KEY, cust_id char(6), purchase_date date, purchase_amount number(10,2), pending_flag number(1), -- 值1 表示它正在等待约束 cust_pur_fk 外键 (cust_id) 引用 CUSTOMERS );
-
那你有什么问题?
-
真的是我第一次做光标程序,所以编译时出错,第二次我不知道我做的是否正确。
-
一个没有参数的过程就是这样,你在过程名称后面的()中没有任何东西。你遇到了什么错误?
标签: oracle stored-procedures plsql procedure database-cursor