-2

我想创建一个包,在包内我们有一个带有参数的函数。我有 TIME_DIM 表,该表的粒度是一条记录/秒。我在装箱时遇到错误,任何人都可以帮助我解决这个问题。

create or replace
PACKAGE PKG_TIME_DIM
IS
 Function FUN_TIME_DESC
   ( TIME_IN IN varchar2 )
   RETURN varchar2
IS
   TIMEDESC varchar2;
   CURSOR c1
   IS
     SELECT TIME_DESC
     from TIME_DIM
     where TIME_DESC = TIME_IN;
BEGIN
   open c1;
   fetch c1 into TIME_DESC;
   if c1%notfound then
      TIMEDESC := 9999;
   end if;
  close c1;
RETURN TIMEDESC;
END;
END;
4

2 回答 2

2

我不知道你得到了什么错误,但我看到了几个问题:通过声明 TIMEDESC 变量,缺少 varchar2 的大小:例如: TIMEDESC varchar2(2000);或更好

TIMEDESC TIME_DIM.TIME_DESC%TYPE;

以下语句中有一个提示

fetch c1 into TIME_DESC;

您已声明TIMEDESC而不是TIME_DESC作为变量的名称

可能是以下问题

TIMEDESC := 9999;

TIMEDESC 是 varchar 类型,但您为其分配了数字

于 2013-07-17T15:14:57.187 回答
2

这里有很多错误。您似乎正在声明一个包含代码的包规范。您需要规范和正文。我在有问题的每一行上添加了评论:

-- Note: "CREATE OR REPLACE PACKAGE" = The specification
CREATE OR REPLACE PACKAGE PKG_TIME_DIM
IS

    FUNCTION FUN_TIME_DESC (TIME_IN IN VARCHAR2) RETURN VARCHAR2;

END PKG_TIME_DIM;
/

-- Note: "CREATE OR REPLACE PACKAGE BODY" = Keyword BODY = the body
CREATE OR REPLACE PACKAGE BODY PKG_TIME_DIM
IS
    FUNCTION FUN_TIME_DESC (TIME_IN IN VARCHAR2) RETURN VARCHAR2
    IS
        TIMEDESC  VARCHAR2(100 CHAR); -- Need to define length of this variable in characters or bytes

        CURSOR c1 IS
          SELECT TIME_DESC
            FROM TIME_DIM
           WHERE TIME_DESC = TIME_IN;
    BEGIN
        OPEN c1;
        FETCH c1 INTO TIMEDESC; -- Changed variable name to match the name you defined

        IF c1%NotFound THEN
            TIMEDESC := '9999'; -- Put quotes around this since this is a VARCHAR
        END IF;

        CLOSE c1;

        RETURN TIMEDESC;

    END FUN_TIME_DESC;

END PKG_TIME_DIM;
/
于 2013-07-17T15:25:45.807 回答