【问题标题】:How to fix 'Error(9,7): PL/SQL: ORA-00947: not enough values' in a function?如何修复函数中的“错误(9,7):PL/SQL:ORA-00947:没有足够的值”?
【发布时间】:2019-05-07 13:36:59
【问题描述】:

我试图为每个销售人员获取总佣金并将其存储到一个函数中以将其放入一个过程中,当我放置一个有效的 select 语句(涉及三个表)时,我得到了 Error(9,20): PL/SQL: ORA-00947: not enough values。我认为是因为 datatype 在此函数中只返回数字,但表中包含导致此问题的其他 varchar 数据类型。

我尝试删除一些 varchar2 数据类型的列,但结果不正确。

以下是我的虚构代码:

create or replace FUNCTION get_total_commission return number 
IS
  v_total_commission;
--
begin
      select
      sale_id, sale_acct,sale_name, sum(commission) as total_commission      
      into v_total from invoice_tbl invoice join commission_tbl commission
      on invoice.id = commission.id join sale_tbl sale on sale.id = commssion.id
      where invoice.refnr is null;
  return to_char(v_total, 'FM99999.00');
EXCEPTION
  WHEN OTHERS THEN
    dbms_output.put_line('err: ' ||SQLERRM);  

end get_total_commission; 

这将是一个功能,将显示每个销售人员赚取的佣金总额。

【问题讨论】:

  • 是的,我知道这是问题之一,我不知道如何返回多个数据类型,因为我不是它的专家......
  • 只有每次销售的总佣金收入...我应该删除包含 varchar 的列以匹配返回数据类型吗?

标签: oracle function plsql procedure


【解决方案1】:

您需要使用 SELECT 列表中的所有四列都返回到的局部变量。并且由于转换为字符类型,需要为函数返回一个字符串类型的值而不是数字。

SQL> SET SERVEROUTPUT ON;
SQL> CREATE OR REPLACE FUNCTION get_total_commission RETURN varchar2 IS
  v_total_commission commission_tbl.commission%type;
  v_sale_id          sale_tbl.sale_id%type;
  v_sale_acct        sale_tbl.sale_acct%type;
  v_sale_name        sale_tbl.sale_name%type;
BEGIN
  select sale_id, sale_acct, sale_name, sum(commission) as total_commission
    into v_sale_id, v_sale_acct, v_sale_name, v_total
    from invoice_tbl invoice
    join commission_tbl commission
      on invoice.id = commission.id
    join sale_tbl sale
      on sale.id = commssion.id
   where invoice.refnr is null;
  return to_char(v_total, 'FM99999.00');
EXCEPTION
  WHEN OTHERS THEN
    dbms_output.put_line('err: ' || SQLERRM);

END;

【讨论】:

    【解决方案2】:

    每位销售人员赚取的佣金总额

    听起来返回一个缺少识别特征的数字并不是您需要的解决方案。你需要一个结果集。就个人而言,这似乎比函数更适合视图,但是如果您想将查询包装在函数中,这就是这样做的方法:

    -- obviously correct these data types to fit your actual needs
    create or replace type commission_t as object(
        sale_id varchar2(30)
        , acct_id varchar2(30)
        , sale_name varchar2(48)
        , total_commission_number
    );
    /
    
    create or replace type commission_nt as table of commission_t; 
    /
    
    create or replace FUNCTION get_total_commission return commission_nt
    IS
        return_value commission_nt;
    begin
          select commission_t(
                       sale_id, sale_acct,sale_name, sum(commission) )   
          bulk collect into return_type
          from invoice_tbl invoice 
               join commission_tbl commission on invoice.id = commission.id 
              join sale_tbl sale on sale.id = commssion.id
          where invoice.refnr is null
          group by sale_id, sale_acct,sale_name
          ;
    
         return return_value;  
    
    end get_total_commission;
    

    并像这样查询它:

    select * from table (get_total_commission);
    

    这有各种粗糙的边缘。例如,如果您的结果集很大(这显然取决于,但说超过 5000-10000 行),它将无法正常工作。


    如果您真的只想要单次销售的总佣金,那么您需要通过 SALE_ID 限制查询 - 并将其作为参数传递:

    create or replace FUNCTION get_total_commission 
        (p_sale_id in sale.id%type)
        return number 
    IS
      v_total_commission number;
    --
    begin
    
          select
          sum(commission) as total_commission      
          into v_total_commission 
          from invoice_tbl invoice 
               join commission_tbl commission on invoice.id = commission.id 
               join sale_tbl sale on sale.id = commssion.id
          where sale.id = p_sale_id
          and invoice.refnr is null;
    
          return v_total_commission ;
    
    end get_total_commission; 
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2020-01-25
      • 1970-01-01
      • 2020-08-16
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多