创建不带参数的过程
Create Procedure with no Parameters
第二次写程序,第一次用游标。对于此过程,我不希望使用参数,但我遇到了问题。这是我目前拥有的:
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;
最好在问题中 post 编辑 CREATE TABLE
语句,而不是评论。此外,您没有 post 所有涉及的表(引用完整性约束引用的表,所以我删除了它们):
SQL> CREATE TABLE customers(
2 cust_id CHAR(6)
3 CONSTRAINT customers_pk PRIMARY KEY,
4 first_name VARCHAR2(100),
5 last_name VARCHAR2(100),
6 credit_line NUMBER(10,2),
7 curr_balance NUMBER(10,2),
8 earned_points NUMBER(5),
9 tier_id CHAR(2)
10 -- CONSTRAINT tier_fk FOREIGN KEY(tier_id)
11 -- REFERENCES rewards_tier,
12 -- CONSTRAINT balance_chk CHECK(curr_balance <= credit_line)
13 );
Table created.
SQL>
SQL> CREATE TABLE purchases(
2 purchase_id CHAR(7)
3 CONSTRAINT purchase_pk PRIMARY KEY,
4 cust_id CHAR(6),
5 purchase_date DATE,
6 purchase_amount NUMBER(10,2),
7 pending_flag NUMBER(1)
8 -- a value of 1 means it is pending
9 -- CONSTRAINT cust_pur_fk FOREIGN KEY(cust_id)
10 -- REFERENCES customers
11 );
Table created.
SQL>
现在,程序:
- 缺少
END LOOP
(所以我添加了它)
- 它包含一对
BEGIN-END
不必要的(所以我删除了它)
- 游标名称是
c1
,但您不通过游标名称引用它 returns 的列 - 您必须使用游标变量名称 (purchaserow
) 而不是
这个编译;你说你不知道你做的对不对。我们怎么知道呢?你从来没有解释过你正在解决什么问题。
SQL> CREATE OR REPLACE PROCEDURE update_balance AS
2 CURSOR c1 IS
3 SELECT purchases.cust_id,
4 purchase_amount,
5 curr_balance,
6 credit_line,
7 pending_flag
8 FROM purchases
9 INNER JOIN customers ON purchases.cust_id = customers.cust_id
10 WHERE purchases.pending_flag = 1
11 FOR UPDATE OF curr_balance;
12
13 pendingbalance purchases.purchase_amount%TYPE;
14 purchaserow c1%rowtype;
15 proposednewbalance purchases.purchase_amount%TYPE;
16 BEGIN
17 OPEN c1;
18 FETCH c1 INTO purchaserow;
19 WHILE c1%found LOOP
20 SELECT SUM(purchase_amount)
21 INTO pendingbalance
22 FROM purchases
23 WHERE cust_id = purchaserow.cust_id -- this
24 AND pending_flag = 1;
25
26 proposednewbalance := pendingbalance + purchaserow.curr_balance;
27 IF proposednewbalance > purchaserow.credit_line THEN
28 dbms_output.put_line('One or more purchases were not processed for Customer');
29 END IF;
30 IF proposednewbalance <= purchaserow.credit_line THEN
31 UPDATE customers
32 SET
33 curr_balance = proposednewbalance
34 WHERE customers.cust_id = purchaserow.cust_id; -- this
35
36 END IF;
37
38 IF proposednewbalance <= purchaserow.credit_line THEN
39 UPDATE purchases
40 SET
41 pending_flag = 1
42 WHERE purchases.cust_id = purchaserow.cust_id;
43
44 END IF;
45 END LOOP; -- this
46 END;
47 /
Procedure created.
SQL>
第二次写程序,第一次用游标。对于此过程,我不希望使用参数,但我遇到了问题。这是我目前拥有的:
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;
最好在问题中 post 编辑 CREATE TABLE
语句,而不是评论。此外,您没有 post 所有涉及的表(引用完整性约束引用的表,所以我删除了它们):
SQL> CREATE TABLE customers(
2 cust_id CHAR(6)
3 CONSTRAINT customers_pk PRIMARY KEY,
4 first_name VARCHAR2(100),
5 last_name VARCHAR2(100),
6 credit_line NUMBER(10,2),
7 curr_balance NUMBER(10,2),
8 earned_points NUMBER(5),
9 tier_id CHAR(2)
10 -- CONSTRAINT tier_fk FOREIGN KEY(tier_id)
11 -- REFERENCES rewards_tier,
12 -- CONSTRAINT balance_chk CHECK(curr_balance <= credit_line)
13 );
Table created.
SQL>
SQL> CREATE TABLE purchases(
2 purchase_id CHAR(7)
3 CONSTRAINT purchase_pk PRIMARY KEY,
4 cust_id CHAR(6),
5 purchase_date DATE,
6 purchase_amount NUMBER(10,2),
7 pending_flag NUMBER(1)
8 -- a value of 1 means it is pending
9 -- CONSTRAINT cust_pur_fk FOREIGN KEY(cust_id)
10 -- REFERENCES customers
11 );
Table created.
SQL>
现在,程序:
- 缺少
END LOOP
(所以我添加了它) - 它包含一对
BEGIN-END
不必要的(所以我删除了它) - 游标名称是
c1
,但您不通过游标名称引用它 returns 的列 - 您必须使用游标变量名称 (purchaserow
) 而不是
这个编译;你说你不知道你做的对不对。我们怎么知道呢?你从来没有解释过你正在解决什么问题。
SQL> CREATE OR REPLACE PROCEDURE update_balance AS
2 CURSOR c1 IS
3 SELECT purchases.cust_id,
4 purchase_amount,
5 curr_balance,
6 credit_line,
7 pending_flag
8 FROM purchases
9 INNER JOIN customers ON purchases.cust_id = customers.cust_id
10 WHERE purchases.pending_flag = 1
11 FOR UPDATE OF curr_balance;
12
13 pendingbalance purchases.purchase_amount%TYPE;
14 purchaserow c1%rowtype;
15 proposednewbalance purchases.purchase_amount%TYPE;
16 BEGIN
17 OPEN c1;
18 FETCH c1 INTO purchaserow;
19 WHILE c1%found LOOP
20 SELECT SUM(purchase_amount)
21 INTO pendingbalance
22 FROM purchases
23 WHERE cust_id = purchaserow.cust_id -- this
24 AND pending_flag = 1;
25
26 proposednewbalance := pendingbalance + purchaserow.curr_balance;
27 IF proposednewbalance > purchaserow.credit_line THEN
28 dbms_output.put_line('One or more purchases were not processed for Customer');
29 END IF;
30 IF proposednewbalance <= purchaserow.credit_line THEN
31 UPDATE customers
32 SET
33 curr_balance = proposednewbalance
34 WHERE customers.cust_id = purchaserow.cust_id; -- this
35
36 END IF;
37
38 IF proposednewbalance <= purchaserow.credit_line THEN
39 UPDATE purchases
40 SET
41 pending_flag = 1
42 WHERE purchases.cust_id = purchaserow.cust_id;
43
44 END IF;
45 END LOOP; -- this
46 END;
47 /
Procedure created.
SQL>