Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在Oracle中,如何在同一列值内的不同模式上使用REPLACE进行更新?_Sql_Oracle_Replace_Sql Update_Oracle12c - Fatal编程技术网

Sql 在Oracle中,如何在同一列值内的不同模式上使用REPLACE进行更新?

Sql 在Oracle中,如何在同一列值内的不同模式上使用REPLACE进行更新?,sql,oracle,replace,sql-update,oracle12c,Sql,Oracle,Replace,Sql Update,Oracle12c,下面是我的表FOO- CREATE TABLE FOO ( NUMBERS VARCHAR2(4000 CHAR) ); INSERT INTO FOO VALUES ('One,Five,Seven'); INSERT INTO FOO VALUES ('One,Two,Three'); INSERT INTO FOO VALUES ('F

下面是我的
表FOO
-

        CREATE TABLE FOO 
           ( 
            NUMBERS VARCHAR2(4000 CHAR)
           );

           INSERT INTO FOO VALUES ('One,Five,Seven');
           INSERT INTO FOO VALUES ('One,Two,Three');
           INSERT INTO FOO VALUES ('Five,Five,Seven');
           INSERT INTO FOO VALUES ('Zero,Five,Seven');
       /*  .
           .
           .
          and so on.. */

SELECT * FROM FOO;

我想写一个update语句,用它们各自的编号替换文本。所以输出应该是-


这里有一种方法。这很愚蠢,但问题也是如此,所以我不觉得太糟糕。它确实有效

update foo
set numbers = (select listagg(decode(token,'Zero',0,'One',1,'Two',2,'Three',3,
                       'Four',4,'Five',5,'Six',6,'Seven',7,'Eight',8,'Nine',9)
                             , ',') within group (order by ord)
               from   json_table('["' || replace(numbers, ',', '","') || '"]',
                                 '$[*]'
                                 columns token varchar2 path '$', 
                                         ord for ordinality)
              )
;

select * from foo;

NUMBERS             
--------------------
1,5,7
1,2,3
5,5,7
0,5,7

这里有一个方法。这很愚蠢,但问题也是如此,所以我不觉得太糟糕。它确实有效

update foo
set numbers = (select listagg(decode(token,'Zero',0,'One',1,'Two',2,'Three',3,
                       'Four',4,'Five',5,'Six',6,'Seven',7,'Eight',8,'Nine',9)
                             , ',') within group (order by ord)
               from   json_table('["' || replace(numbers, ',', '","') || '"]',
                                 '$[*]'
                                 columns token varchar2 path '$', 
                                         ord for ordinality)
              )
;

select * from foo;

NUMBERS             
--------------------
1,5,7
1,2,3
5,5,7
0,5,7

这是一个更愚蠢的攻击(仍然正确-它应该在Oracle 12.1及更高版本中工作)。这是一个更有趣的例子,说明什么是可能的

update /*+ with_plsql */ foo
  set numbers = (
    with
      function list_replace(str varchar2) return varchar2 as
        p integer := instr(str,',');
        function single_replace(token varchar2) return varchar2 as
        begin
          return case token when 'Zero'  then '0' when 'One'   then '1'
                            when 'Two'   then '2' when 'Three' then '3'
                            when 'Four'  then '4' when 'Five'  then '5'
                            when 'Six'   then '6' when 'Seven' then '7'
                            when 'Eight' then '8' when 'Nine'  then '9' end;
        end single_replace;
      begin
        return case p when 0 then single_replace(str)
                      else single_replace(substr(str,1,p-1)) || ',' ||
                           list_replace(substr(str,p+1)) end;
      end list_replace;
    select list_replace(numbers) from dual
  )
/

这是一个更愚蠢的攻击(仍然正确-它应该在Oracle 12.1及更高版本中工作)。这是一个更有趣的例子,说明什么是可能的

update /*+ with_plsql */ foo
  set numbers = (
    with
      function list_replace(str varchar2) return varchar2 as
        p integer := instr(str,',');
        function single_replace(token varchar2) return varchar2 as
        begin
          return case token when 'Zero'  then '0' when 'One'   then '1'
                            when 'Two'   then '2' when 'Three' then '3'
                            when 'Four'  then '4' when 'Five'  then '5'
                            when 'Six'   then '6' when 'Seven' then '7'
                            when 'Eight' then '8' when 'Nine'  then '9' end;
        end single_replace;
      begin
        return case p when 0 then single_replace(str)
                      else single_replace(substr(str,1,p-1)) || ',' ||
                           list_replace(substr(str,p+1)) end;
      end list_replace;
    select list_replace(numbers) from dual
  )
/

你是如何想出两个答案的?我连一个都找不到!!!你能解释一下这两个答案之间的区别吗?你是如何得出这两个答案的?我连一个都找不到!!!你能解释一下这两个答案的区别吗?