我想创建一个包,在包中我们有一个带参数的函数。我有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;
答案 0 :(得分:2)
我不知道你得到了什么错误,但我看到了几个问题:
通过声明TIMEDESC变量,缺少varchar2的大小:
例如:TIMEDESC varchar2(2000);
或更好
TIMEDESC TIME_DIM.TIME_DESC%TYPE;
以下陈述中有一个便条
fetch c1 into TIME_DESC;
您已将TIMEDESC
而非TIME_DESC
声明为变量名称
可能是以下是一个问题
TIMEDESC := 9999;
TIMEDESC的类型为varchar,但您为其指定了编号
答案 1 :(得分: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;
/