我正在尝试定义PL / SQL函数
CREATE OR REPLACE FUNCTION B2BOWNER.F_SSC_Page_Map_Select(
p_page_id IN B2BOWNER.SSC_Page_Map.PAGE_ID_NBR%TYPE,
p_page_type IN B2BOWNER.SSC_Page_Map.PAGE_TYPE%TYPE,
p_page_dcpn IN B2BOWNER.SSC_Page_Map.PAGE_DCPN%TYPE)
RETURN MAP_REC
AS
CURSOR MAP_CURSOR IS
SELECT *
FROM B2BOWNER.SSC_PAGE_MAP
WHERE PAGE_ID_NBR = p_page_id AND PAGE_TYPE = p_page_type;
MAP_REC MAP_CURSOR%ROWTYPE;
TABLE_DOES_NOT_EXIST exception;
PRAGMA EXCEPTION_INIT(TABLE_DOES_NOT_EXIST, -942); -- ORA-00942
BEGIN
FOR MAP_REC IN MAP_CURSOR
LOOP
System.out.println("ID: " + MAP_REC.PAGE_ID_NBR + " " + "TYPE: " + MAP_REC.PAGE_TYPE + " " + "DCPN: " + MAP_REC.PAGE_DCPN);
END LOOP;
RETURN MAP_REC;
EXCEPTION
WHEN TABLE_DOES_NOT_EXIST THEN
RETURN -1;
WHEN DUP_VAL_ON_INDEX THEN
RETURN -2;
WHEN INVALID_NUMBER THEN
RETURN -3;
WHEN OTHERS THEN
RETURN -4;
END F_SSC_Page_Map_Select;
SHOW ERRORS PROCEDURE B2BOWNER.F_SSC_Page_Map_Select;
GRANT EXECUTE ON B2BOWNER.F_SSC_Page_Map_Select TO B2B_USER_DBROLE;
并收到以下错误
Warning: compiled but with compilation errors
No errors.
Grant complete.
[Warning] ORA-24344: success with compilation error
6/12 PLS-00320: the declaration of the type of this expression is incomplete or malformed
PL/SQL: Compilation unit analysis terminated
(1: 0): Warning: compiled but with compilation errors
答案 0 :(得分:1)
一些事情。首先,声明部分中声明的MAP_REC 不与游标FOR循环中使用的MAP_REC相同。当我第一次遇到它时,这对我来说当然是令人惊讶的,但这是我们都必须习惯的东西。要做你正在尝试做的事情,你需要使用OPEN,FETCH和CLOSE方法处理游标,或者将游标FOR循环变量中的值复制到&#39 ;宣称'变量
其次,由于MAP_REC是在函数内部声明的,因此您无法从此函数返回MAP_REC,因此编译器在处理函数定义时也不知道。最好使用特定的表ROWTYPE。
第三,稍后在代码中你有RETURN -1
等,它不能作为游标%ROWTYPE变量工作。我建议不要试图返回魔术数字'用于指示特定故障的值,您应该简单地将异常传播给调用者,然后调用者可以根据需要处理异常。这就是我们有异常的原因 - 防止为每个子程序设置不同的错误处理方案。
最后:这是PL / SQL - 我们这里没有System.out.println。 : - )
重写代码的一种可能方法是:
CREATE OR REPLACE FUNCTION B2BOWNER.F_SSC_Page_Map_Select(
p_page_id IN B2BOWNER.SSC_Page_Map.PAGE_ID_NBR%TYPE,
p_page_type IN B2BOWNER.SSC_Page_Map.PAGE_TYPE%TYPE,
p_page_dcpn IN B2BOWNER.SSC_Page_Map.PAGE_DCPN%TYPE)
RETURN B2BOWNER.SSC_PAGE_MAP%ROWTYPE
AS
CURSOR MAP_CURSOR IS
SELECT *
FROM B2BOWNER.SSC_PAGE_MAP
WHERE PAGE_ID_NBR = p_page_id AND
PAGE_TYPE = p_page_type;
MAP_REC B2BOWNER.SSC_PAGE_MAP%ROWTYPE;
bCursor_open BOOLEAN := FALSE;
BEGIN
OPEN MAP_CURSOR;
bCursor_open := TRUE;
LOOP
FETCH MAP_CURSOR
INTO MAP_REC;
EXIT WHEN MAP_CURSOR%NOT_FOUND;
DBMS_OUTPUT.PUT_LINE('ID: ' || MAP_REC.PAGE_ID_NBR || ' ' || 'TYPE: ' ||
MAP_REC.PAGE_TYPE || ' ' || 'DCPN: ' || MAP_REC.PAGE_DCPN);
END LOOP;
CLOSE MAP_CURSOR;
bCursor_open := FALSE;
RETURN MAP_REC;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error in F_SSC_Page_Map_Select: ' || SQLCODE || ' ' ||
SQLERRM);
IF bCursor_open THEN
CLOSE MAP_CURSOR;
END IF;
RAISE;
END F_SSC_Page_Map_Select;
祝你好运。
分享并享受。