我有使用本地定义集合的代码:
CREATE OR REPLACE FUNCTION WBAR_COUNT_TEST(IB_TAKE_WAITING IN BOOLEAN DEFAULT TRUE,
IB_TAKE_ERROR IN BOOLEAN DEFAULT TRUE,
IB_TAKE_KILLED IN BOOLEAN DEFAULT TRUE,
IB_TAKE_SUCCESS IN BOOLEAN DEFAULT FALSE)
RETURN PLS_INTEGER IS
SUBTYPE ST_JOB_STATUS IS JOB_STATUS_HISTORY.STATUS%TYPE;
TYPE TTV_STATUSES IS TABLE OF ST_JOB_STATUS;
LTV_STATUSES TTV_STATUSES := TTV_STATUSES();
LN_RETURN_CODE PLS_INTEGER := 0;
PROCEDURE ADD_STATUS(IB_FLAG IN BOOLEAN,
IV_STATUS IN ST_JOB_STATUS) IS
BEGIN
IF IB_FLAG THEN
LTV_STATUSES.EXTEND;
LTV_STATUSES(LTV_STATUSES.LAST) := IV_STATUS;
END IF;
END ADD_STATUS;
BEGIN
ADD_STATUS(IB_TAKE_WAITING, 'WAITING');
ADD_STATUS(IB_TAKE_ERROR, 'ERROR');
ADD_STATUS(IB_TAKE_KILLED, 'KILLED');
ADD_STATUS(IB_TAKE_SUCCESS, 'SUCCESS');
SELECT COUNT(1)
INTO LN_RETURN_CODE
FROM JOB_STATUS_HISTORY H
WHERE H.STATUS IN (LTV_STATUSES);
RETURN LN_RETURN_CODE;
END WBAR_COUNT_TEST;
我收到了:PLS-00642: local collection types not allowed in SQL statements
有没有办法在TTV_STATUSES
中将Oracle 11g R2
定义为全局类型?
答案 0 :(得分:0)
除非你轻视这个例子,否则我不会为创建本地类型而烦恼,只需使用动态SQL。这个特殊的例子非常安全,因为你可以控制插入的文本,并且在4个变量的限制下,你最多可以在缓存中存储15个查询,所以这里也没有什么大不了的:
CREATE OR REPLACE FUNCTION WBAR_COUNT_TEST(IB_TAKE_WAITING IN BOOLEAN DEFAULT TRUE,
IB_TAKE_ERROR IN BOOLEAN DEFAULT TRUE,
IB_TAKE_KILLED IN BOOLEAN DEFAULT TRUE,
IB_TAKE_SUCCESS IN BOOLEAN DEFAULT FALSE)
RETURN PLS_INTEGER IS
-- Character String large enough to hold maximum string length
LTV_STATUSES VARCHAR2(40) := NULL;
LN_RETURN_CODE PLS_INTEGER := 0;
PROCEDURE ADD_STATUS(IB_FLAG IN BOOLEAN,
IV_STATUS IN ST_JOB_STATUS) IS
BEGIN
IF IB_FLAG THEN
-- Concatenate desired values togather in IN-LIST format
IF LTV_STATUSES IS NULL THEN
LTV_STATUSES := ''''||IV_STATUS||'''';
ELSE
LTV_STATUSES := ','''||IV_STATUS||'''';
END IF;
END IF;
END ADD_STATUS;
BEGIN
ADD_STATUS(IB_TAKE_WAITING, 'WAITING');
ADD_STATUS(IB_TAKE_ERROR, 'ERROR');
ADD_STATUS(IB_TAKE_KILLED, 'KILLED');
ADD_STATUS(IB_TAKE_SUCCESS, 'SUCCESS');
-- Makes sure we don't try and run the query with NO values selected
IF LTV_STATUSES IS NOT NULL THEN
EXECUTE IMMEDIATE 'SELECT COUNT(1) FROM JOB_STATUS_HISTORY H WHERE H.STATUS in ('||LTV_STATUSES||')' into LN_RETURN_CODE;
END IF;
RETURN LN_RETURN_CODE;
END WBAR_COUNT_TEST;