没有参数的创建过程

时间:2018-12-08 12:58:52

标签: oracle stored-procedures plsql procedure database-cursor

这是我第二次编写程序,而这是我第一次必须使用游标。对于此过程,我不希望使用参数,但是我有问题。这是我目前拥有的:

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;

1 个答案:

答案 0 :(得分:1)

如果您在问题中发布CREATE TABLE语句而不是发表评论,那会更好。此外,您没有发布所有涉及的表(那些引用完整性约束引用了这些表,因此我将它们删除了):

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,但是您不引用其按游标名称返回的列-您必须使用游标变量名称(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>