【问题标题】:ORA-06575: Package or function FUNC_GETCREATEDTIME is in an invalid stateORA-06575: 包或函数 FUNC_GETCREATEDTIME 处于无效状态
【发布时间】:2020-05-07 14:02:45
【问题描述】:

我第一次尝试像这样创建一个函数:

create or replace function func_getcreatedtime(p_createdtime varchar2)
return varchar2 as
    formatted_time varchar2(1000) := '';
begin
    select case formatted_time 
    when TO_CHAR(sysdate, 'YYYY') = TO_CHAR(p_createdtime, 'YYYY') 
    then TO_CHAR(p_createdtime, 'Mon DD') || ' at ' || TO_CHAR(p_createdtime, 'HH24:MI') createdtime
    else TO_CHAR(p_createdtime, 'Mon DD, YYYY') || ' at ' || TO_CHAR(p_createdtime, 'HH24:MI') createdtime,
    end
from dual;

return formatted_time;
end;

并尝试像这样调用此函数:

select t.comments, t.createdtime, func_getcreatedtime(t.createdtime) from postcomment t;

但它显示此错误:

ORA-06575: 包或函数 FUNC_GETCREATEDTIME 处于无效状态

t.createdtime30-APR-20 06.59.59.546000 PM 格式,我想格式化这个日期时间。

如果 sysdate 和 t.createdtime 的年份相同,那么我想将其格式化为 Apr 30 at 06:59

如果 sysdate 和 t.createdtime 的年份不同,那么我想将其格式化为 Apr 30, 2020 at 06:59

【问题讨论】:

  • 您不需要那里的选择,您可以直接将 to_char 的值分配给变量。至于问题 - 在你的情况下编译失败,因为有几个语法错误
  • 你用的是什么开发工具?您需要熟悉它显示编译错误的方式。
  • @WilliamRobertson PL/SQL 开发人员
  • @ShreyasPednekar 绝佳选择。在这种情况下,请确保使用程序窗口来开发功能。初学者的错误是使用 SQL 窗口,它不是为 PL/SQL 代码设计的,也不显示编译错误。

标签: oracle function plsql


【解决方案1】:

使用SELECT INTO formatted_time创建如下函数

    create or replace function func_getcreatedtime(p_createdtime varchar2)
    return varchar2 as
        formatted_time varchar2(1000) := '';
        format_date timestamp; 
    begin
        format_date:=to_timestamp(p_createdtime,'DD-MON-YY HH.MI.SS.FF PM');

        select case  
        when TO_CHAR(sysdate, 'YYYY') = TO_CHAR(format_date, 'YYYY') 
        then TO_CHAR(format_date, 'Mon DD') || ' at ' || TO_CHAR(format_date, 'HH24:MI') 
        else TO_CHAR(format_date, 'Mon DD, YYYY') || ' at ' || TO_CHAR(format_date, 'HH24:MI') 
        end as created_time
        INTO formatted_time
    from dual;

    return formatted_time;
    end;

【讨论】:

  • 显示错误ORA-01722: invalid number ORA-06512: at "PHARMINAR.FUNC_GETCREATEDTIME", line 5 即这一行select case
  • 好的,我尝试了同样的方法,它成功了。感谢您的帮助。
【解决方案2】:

这个错误意味着函数要么有错误,要么函数调用的东西已经改变并且数据库使函数无效以强制它被编译。

在这种情况下,您的函数有错误。可以通过执行查看错误

SELECT *
  FROM USER_ERRORS
  WHERE NAME = 'FUNC_GETCREATEDTIME`

您会在哪里找到以下内容:

FUNC_GETCREATEDTIME FUNCTION    1   6   35  PL/SQL: ORA-00905: missing keyword  ERROR   0
FUNC_GETCREATEDTIME FUNCTION    2   5   5   PL/SQL: SQL Statement ignored   ERROR   0
FUNC_GETCREATEDTIME FUNCTION    3   13  3   PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:

   ; <an identifier> <a double-quoted delimited-identifier>
   current delete exists prior <a single-quoted SQL string>
The symbol ";" was substituted for "end-of-file" to continue.
ERROR   103

See this db<>fiddle

【讨论】:

  • 如果您查看我在问题中发布的 dbfiddle 链接,您会发现有几个错误,我已经编辑了答案并在此处添加了错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-17
相关资源
最近更新 更多