2

我有使用本地定义集合的代码:

 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吗?

4

1 回答 1

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;
于 2013-10-24T16:17:10.353 回答