Oracle 12c 使用相同的数据更新多行

Oracle 12c updating multiple rows with the same data

我正在尝试编写一个脚本,提取字符串中所有不同的 'words' 并将它们保存在另一个字段中。我已经得到了在 Oracle 19c 中工作的过程(尽管欢迎任何建议)但是当我 运行 12c 中的脚本时,第一条记录是正确的,但以下所有记录都具有相同的数据,我不确定我做错了什么。

谢谢!

drop table temp purge;
create table temp (A CHAR(1), S1 varchar(32), S2 varchar(32));
commit;

insert into temp (A,S1)
select 'A', '1 2 3 4 1 2 3 4 1 2 3 4' from dual;
commit;
insert into temp (A,s1)
select 'B', '6 7 8 9 6 7 8 9 6 7 8 9 6 7 8 9' from dual;
commit;
insert into temp (A,s1)
select 'C', 'A B C D A B C D' from dual;
commit;

select * from temp;

UPDATE temp set (S2) = (
    SELECT LISTAGG(str, ' ') WITHIN GROUP (ORDER BY str) str
    FROM (
        SELECT DISTINCT REGEXP_SUBSTR(S1, '[^ ]+', 1, LEVEL) AS str FROM dual
        CONNECT BY REGEXP_SUBSTR(S1, '[^ ]+', 1, LEVEL) IS NOT NULL
    )
);


select * from temp;

输出:

A S1                               S2
- -------------------------------- --------------------------------
A 1 2 3 4 1 2 3 4 1 2 3 4
B 6 7 8 9 6 7 8 9 6 7 8 9 6 7 8 9
C A B C D A B C D


3 rows updated.


A S1                               S2
- -------------------------------- --------------------------------
A 1 2 3 4 1 2 3 4 1 2 3 4          1 2 3 4
B 6 7 8 9 6 7 8 9 6 7 8 9 6 7 8 9  1 2 3 4
C A B C D A B C D                  1 2 3 4

预计:

A S1                               S2
- -------------------------------- --------------------------------
A 1 2 3 4 1 2 3 4 1 2 3 4          1 2 3 4
B 6 7 8 9 6 7 8 9 6 7 8 9 6 7 8 9  6 7 8 9
C A B C D A B C D                  A B C D

我不知道问题出在哪里,但这里有一个替代解决方案可以避免使用 REGEXP 的 CPU 惩罚:

update temp set s2 =
xmlcast(
  xmlquery(
    'string-join(distinct-values(tokenize($X, " ")), " ")'
    passing s1 as X returning content
  )
  as varchar2(64)
);

请尝试以下操作,它应该有效 -

update (
select temp.*
      ,(SELECT LISTAGG(str, ' ') WITHIN GROUP (ORDER BY str) str
          FROM (
                SELECT DISTINCT REGEXP_SUBSTR(S1, '[^ ]+', 1, LEVEL) AS str FROM dual
                CONNECT BY REGEXP_SUBSTR(S1, '[^ ]+', 1, LEVEL) IS NOT NULL
               )
        ) result
  from temp
)
set S2 = result

此处的关键是确保为每一行计算 S2。