【问题标题】:Create Procedure with no Parameters创建没有参数的过程
【发布时间】: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


【解决方案1】:

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

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>

【讨论】:

  • Littlefoot 非常感谢您!所以我假设我在正确的轨道上只是在某些地方出现了一些错误的语法?
  • Littlefoot 我可以再利用你的知识吗? stackoverflow.com/questions/53773756/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多