我正在 PLSQL 中构建我的第一个函数,它需要根据参数用户名和会计年度返回用户支出的单个总和值

发布于 2025-01-12 00:13:35 字数 1462 浏览 0 评论 0原文

该函数编译,但当我运行此块时:

 select sumyUserSpending('ALCraft', 15) from infor.credit_card_transaction

它抛出错误ORA-06575:包或函数 sumyuserspending 处于无效状态

当我运行此块时:

 declare
    answer number;
    begin
      answer := sumyUserSpending('ALCraft', 15);
      dbms_output.put_line(answer);
      end;

它抛出错误 ORA-65550 和 PLS-00905:对象 sumyuserspending 无效

这是我的功能。当我只运行该函数时,它不会抛出任何错误,因此我不知道它需要什么才能顺利运行。当从函数中取出查询并单独运行查询时,它会根据我为参数输入的占位符值返回我想要的值。我在 oracle 12c 中工作。

create or replace function sumyUserSpending (userN in varchar2, fiscYear in number)
  return number
  is
  total number;
  
  cursor search1 is
  select sum(infor.credit_card_transaction.due_cc_co_amount) into total from infor.credit_card_transaction
    inner join infor.credit_card using (credit_card_id)
    inner join infor.ext_user using (user_id)
    where infor.credit_card_transaction.transaction_date > concat('01-JUL-', (fiscYear - 1))
    and infor.credit_card_transaction.transaction_date < concat('30-JUNE-', fiscYear) 
    and NVL(SUBSTR(infor.ext_user.email_address, 0, INSTR(infor.ext_user.email_address, '@')-1), infor.ext_user.email_address) = userN;
    
    begin
      open search1
      fetch search1 into total;
      
      if search1%notfound then
        total := 0;
        end if;
        
        close search1;
        
        return total;
        end;

The function compiles, but when I run this block:

 select sumyUserSpending('ALCraft', 15) from infor.credit_card_transaction

It throws error ORA-06575: Package or function sumyuserspending is in an invalid state.

When I run this block:

 declare
    answer number;
    begin
      answer := sumyUserSpending('ALCraft', 15);
      dbms_output.put_line(answer);
      end;

It throws errors ORA-65550 and PLS-00905: object sumyuserspending is invalid.

Here is my function. When I only run the function, it throws no errors, so I am lost as to what it could need to run smoothly. When the query is taken out of the function and I run the query alone, it returns the value I want based on the placeholder values I put in for the parameters. I am working in oracle 12c.

create or replace function sumyUserSpending (userN in varchar2, fiscYear in number)
  return number
  is
  total number;
  
  cursor search1 is
  select sum(infor.credit_card_transaction.due_cc_co_amount) into total from infor.credit_card_transaction
    inner join infor.credit_card using (credit_card_id)
    inner join infor.ext_user using (user_id)
    where infor.credit_card_transaction.transaction_date > concat('01-JUL-', (fiscYear - 1))
    and infor.credit_card_transaction.transaction_date < concat('30-JUNE-', fiscYear) 
    and NVL(SUBSTR(infor.ext_user.email_address, 0, INSTR(infor.ext_user.email_address, '@')-1), infor.ext_user.email_address) = userN;
    
    begin
      open search1
      fetch search1 into total;
      
      if search1%notfound then
        total := 0;
        end if;
        
        close search1;
        
        return total;
        end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱暴击 2025-01-19 00:13:35
  • 您需要仅通过表名或 FROM 子句之外的别名来引用表,而不是通过 schema_name.table_name 来引用这些表。
  • 不要依赖字符串到日期的隐式转换;使用TO_DATE显式转换它们。
  • 如果您使用游标,则 INTO 子句在语法上无效。
  • 但是,您不需要光标。

修复所有问题,为您提供:

create or replace function sumyUserSpending (
  userN    in ext_user.email_address%TYPE,
  fiscYear in number
) RETURN number
IS
  total number;
BEGIN  
  select COALESCE(sum(cct.due_cc_co_amount), 0)
  into total
  from infor.credit_card_transaction cct
       inner join infor.credit_card cc using (credit_card_id)
       inner join infor.ext_user eu    using (user_id)
  where cct.transaction_date >= TO_DATE((fiscyear - 1) || '07-01', 'RR-MM-DD')
  and   cct.transaction_date <  TO_DATE(fiscyear || '07-01', 'RR-MM-DD')
  and   NVL(SUBSTR(eu.email_address, 0, INSTR(eu.email_address, '@')-1), eu.email_address)
          = userN;

  return total;
end;
/

db<>fiddle 此处

  • You need to refer to the tables by their just table name or an alias outside the FROM clause and not by schema_name.table_name.
  • Do not rely on implicit conversion of strings to dates; explicitly convert them using TO_DATE.
  • If you are using a cursor then a INTO clause is not syntactically valid.
  • However, you do not need a cursor.

Fixing all that gives you:

create or replace function sumyUserSpending (
  userN    in ext_user.email_address%TYPE,
  fiscYear in number
) RETURN number
IS
  total number;
BEGIN  
  select COALESCE(sum(cct.due_cc_co_amount), 0)
  into total
  from infor.credit_card_transaction cct
       inner join infor.credit_card cc using (credit_card_id)
       inner join infor.ext_user eu    using (user_id)
  where cct.transaction_date >= TO_DATE((fiscyear - 1) || '07-01', 'RR-MM-DD')
  and   cct.transaction_date <  TO_DATE(fiscyear || '07-01', 'RR-MM-DD')
  and   NVL(SUBSTR(eu.email_address, 0, INSTR(eu.email_address, '@')-1), eu.email_address)
          = userN;

  return total;
end;
/

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文