oracle12c用相同的数据更新多行

xxb16uws  于 2021-08-01  发布在  Java
关注(0)|答案(2)|浏览(341)

我试着写一个脚本,将字符串中所有不同的“单词”都提取出来,并将它们保存在另一个字段中。我已经让这个过程在oracle19c中运行(尽管欢迎任何建议),但是当我在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
q0qdq0h2

q0qdq0h21#

我不知道是什么问题,但这里有一个替代解决方案,可以避免使用regexp带来的cpu损失:

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

omjgkv6w2#

请尝试以下,它应该工作-

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。

相关问题