在Oralcle函数中使用带有CONCAT函数的subselect

时间:2016-07-16 11:49:22

标签: plsql oracle11g concat pls-00103

使用CONCAT功能时,Oracle SQL和PL / SQL的规则是否不同?如何在Oracle中使用带有函数的subselect for CONCAT函数。

如果我有一个简单的查询,如下所示,它可以正常工作

SELECT CONCAT (' A, B.',
               (SELECT REGEXP_SUBSTR ('CC, DDD, E, ',
                                      '[^,]+',
                                      1,
                                      1)
                  --INTO RESULT_PATTERN
                  FROM DUAL))
  FROM DUAL;

但是,如果我在PL / SQL函数中添加它,我会收到错误

   FUNCTION TEST_SELECTION (BIT_PATTERN   IN VARCHAR2,
                                 SEL_PATTERN   IN VARCHAR2)
      RETURN VARCHAR2
   IS
      RESULT_PATTERN     VARCHAR2 (250);
      LOOP_BIT_PATTERN   VARCHAR2 (10);
      LOOP_SEL_PATERN    VARCHAR2 (300);
      TEMP_VAR           VARCHAR2 (200);
   BEGIN
      IF (LENGTH (BIT_PATTERN) > 0 AND SUBSTR (BIT_PATTERN, 0, 1) = '1')
      THEN
         RESULT_PATTERN :=
            CONCAT (RESULT_PATTERN,
                    (SELECT REGEXP_SUBSTR (SEL_PATTERN,   **--This is line 35**
                                           '[^,]+',
                                           1,
                                           1)
                               --TEMP_VAR
                       FROM DUAL));      **--This is line 40**
         BIT_PATTERN := SUBSTR (BIT_PATTERN, 1);
         SEL_PATTERN := '';
         --TEST_SELECTION (BIT_PATTERN, SEL_PATTERN);
      END IF;
    END TEST_SELECTION;

错误是

[Error] PLS-00103 (35: 22): PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:

   ( - + case mod new not null <an identifier>
   <a double-quoted delimited-identifier> <a bind variable>
   continue
[Error] PLS-00103 (40: 33): PLS-00103: Encountered the symbol ")" when expecting one of the following:

   . , @ ; for <an identifier>
   <a double-quoted delimited-identifier> group having intersect
   minus order partition sta

1 个答案:

答案 0 :(得分:1)

您忘记在块的末尾返回值。

在你的情况下,没有必要在pl块中选择选择...来自双

-- by default all parameters are IN
FUNCTION TEST_SELECTION (BIT_PATTERN VARCHAR2,
                         SEL_PATTERN VARCHAR2)
RETURN VARCHAR2
IS
    RESULT_PATTERN     VARCHAR2 (250);
    LOOP_BIT_PATTERN   VARCHAR2 (10);
    LOOP_SEL_PATERN    VARCHAR2 (300);
BEGIN
    IF (LENGTH (BIT_PATTERN) > 0 AND SUBSTR (BIT_PATTERN, 0, 1) = '1')
    THEN
        -- Are you sure RESULT_PATTERN has a value at this point?
        RESULT_PATTERN := RESULT_PATTERN || 
                          REGEXP_SUBSTR(SEL_PATTERN, '[^,]+', 1,1);
        BIT_PATTERN := SUBSTR(BIT_PATTERN, 1);
        SEL_PATTERN := '';
    END IF;
    -- I supose you want to return this variable
    RETURN RESULT_PATTERN;
END TEST_SELECTION;