【问题标题】:Loop over a each value wrapped up by listagg遍历 listagg 包裹的每个值
【发布时间】:2022-11-17 21:33:25
【问题描述】:

我有一个表,其中包含一个名为 column_names 的列,对于每一行,它都有一个用逗号分隔的列名列表,例如:

table column_names
some DEPOSITS_COUNT,DEPOSITS

我想遍历列名中的每个命名字符串,这是我尝试过的:

DECLARE
  deposits VARCHAR(255);
BEGIN
   FOR DEPO IN ( SELECT REGEXP_SUBSTR (
   column_names, '[^,]+') "REGEXPR_SUBSTR" INTO deposits
 FROM REFERENCE_TEST )
   LOOP
      DBMS_OUTPUT.PUT_LINE (DEPO.deposits);
   END LOOP;
END;

我使用了DECLARE,因为我一直收到DEPO没有声明的错误。

我得到的错误:

Error report -
ORA-06550: line 8, column 34:
PLS-00302: component 'DEPOSITS' must be declared
ORA-06550: line 8, column 7:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

【问题讨论】:

    标签: sql oracle-sqldeveloper


    【解决方案1】:

    不要在游标内使用 SELECT ... INTO ...

    BEGIN
       FOR DEPO IN (
         SELECT REGEXP_SUBSTR (column_names, '[^,]+') "REGEXPR_SUBSTR"
         FROM   REFERENCE_TEST
       )
       LOOP
          DBMS_OUTPUT.PUT_LINE (DEPO.deposits);
       END LOOP;
    END;
    /
    

    这将使您获得每一行中的第一个值。

    【讨论】:

      猜你喜欢
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多