oracle存储过程以获得给定参数的总和(余额)

时间:2012-03-15 04:15:19

标签: oracle stored-procedures plsql

我有一个supplier_product表(supp_id,prod_id,invoice_id,price)和一张发票表(invoice_id,balance)。我试过一个存储过程。给(supp_id)它应该是所有现有的invoice_id并显示余额。这是我的代码:

set serverouput on;
create or replace
Procedure SUP_loop
(v_SUPPLIER_ID int   )
AS
  CURSOR c_SUP IS
    select  SUPPLIER_ID , SUPP_INVOICE_ID, balance 
      from SUPPLIER_PRODUCT, supplier_invoice
     where SUPPLIER_ID=v_SUPPLIER_ID 
       and supp_invoice_id.supplier_product=supp_invoice_id.supplier_invoice;
BEGIN
  --LOOP WITH IMPLICIT VARIABLE DECLARED 
  --AUTOMATIC, OPEN FETCH, CLOSE

  FOR v_SUP_data IN c_SUP LOOP
    DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_SUP_data.SUPPLIER_ID) || '   ' || 
                             TO_CHAR(v_SUP_data.SUPP_INVOICE_ID) || '   ' || 
                             TO_CHAR(v_SUP_data.balance)  );
  END LOOP;
END;
/

我得到的错误是v_sup_data错误(20,31):PLS-00364:循环索引变量'V_SUP_DATA'使用无效

Error(9,74): PL/SQL: ORA-00904: "SUPP_INVOICE_ID"."SUPPLIER_INVOICE": invalid identifier

2 个答案:

答案 0 :(得分:2)

你有错误的方式交换字段和表名。

你有......

supp_invoice_id.supplier_invoice

......你应该拥有......

supplier_invoice.supp_invoice_id

:d

答案 1 :(得分:0)

引用列的语法是<>。<&gt ;.因此,您的游标查询需要连接条件为supplier_produce.supp_invoice_id = supplier_invoice.supp_invoice_id,即

create or replace
Procedure SUP_loop
(v_SUPPLIER_ID int   )
AS
  CURSOR c_SUP IS
    select  SUPPLIER_ID , SUPP_INVOICE_ID, balance 
      from SUPPLIER_PRODUCT, supplier_invoice
     where SUPPLIER_ID=v_SUPPLIER_ID 
       and supplier_product.supp_invoice_id = supplier_invoice.supp_invoice_id;
BEGIN
  --LOOP WITH IMPLICIT VARIABLE DECLARED 
  --AUTOMATIC, OPEN FETCH, CLOSE

  FOR v_SUP_data IN c_SUP LOOP
    DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_SUP_data.SUPPLIER_ID) || '   ' || 
                             TO_CHAR(v_SUP_data.SUPP_INVOICE_ID) || '   ' || 
                             TO_CHAR(v_SUP_data.balance)  );
  END LOOP;
END;
/