Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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-从一个表插入到另一个表,但操作数据_Sql_Database_Oracle - Fatal编程技术网

SQL-从一个表插入到另一个表,但操作数据

SQL-从一个表插入到另一个表,但操作数据,sql,database,oracle,Sql,Database,Oracle,我正在尝试使用以下脚本将数据从一个表复制到另一个表: insert into test_report ( company_id , report_id , brch_code , definition , description , editable_flag , executable_flag , name , report_type ) values ( 2420 , 'RP00002004' , '0001' , (select definition from test_template

我正在尝试使用以下脚本将数据从一个表复制到另一个表:

insert into test_report
( company_id
, report_id
, brch_code
, definition
, description
, editable_flag
, executable_flag
, name
, report_type ) 
values
( 2420
, 'RP00002004'
, '0001'
, (select definition from test_template_report where template_id='RP00001242')
, (select description from test_template_report where template_id='RP00001242')
, (select editable_flag from test_template_report where template_id='RP00001242')
, (select executable_flag from test_template_report where template_id='RP00001242')
, (select name from test_template_report where template_id='RP00001242')
, '01' );
这很好,但是定义字段包含XML,需要稍微修改一下

以下是定义数据的一部分:

<listdef page='25'><reportId>RP00000390</reportId><name>Fund Transfer</name><description>Fund Transfer</description>
根据插入脚本,RP00000390部分需要更改为RP000002004

例如:

<listdef page='25'><reportId>RP00002004</reportId><name>Fund Transfer</name><description>Fund Transfer</description>

这可能吗?

您可以将XMLQuery与修改。。。替换节点的值:

您不需要从模板表中进行所有单独的选择,只需一次插入选择即可

XML操作假定定义为XMLType;如果不是,您可以在passing子句中将其转换为一,即将XMLTypedefinition作为d传递。reportId节点的值将替换为作为r传递的字符串

作为替换发生的快速静态演示,使用以字符串文本形式在线提供的XML:

select
  XMLQuery('copy $i := $d modify
      (for $j in $i//reportId return replace value of node $j with $r)
      return $i'
    passing XMLType(q'[<listdef page='25'><reportId>RP00000390</reportId><name>Fund Transfer</name><description>Fund Transfer</description></listdef>]') as "d",
      'RP00002004' as "r"
    returning content)
  as modified_definition
from dual;

MODIFIED_DEFINITION                                                                                                           
------------------------------------------------------------------------------------------------------------------------------
<listdef page="25"><reportId>RP00002004</reportId><name>Fund Transfer</name><description>Fund Transfer</description></listdef>

.

您可以使用带有修改。。。替换节点的值:

您不需要从模板表中进行所有单独的选择,只需一次插入选择即可

XML操作假定定义为XMLType;如果不是,您可以在passing子句中将其转换为一,即将XMLTypedefinition作为d传递。reportId节点的值将替换为作为r传递的字符串

作为替换发生的快速静态演示,使用以字符串文本形式在线提供的XML:

select
  XMLQuery('copy $i := $d modify
      (for $j in $i//reportId return replace value of node $j with $r)
      return $i'
    passing XMLType(q'[<listdef page='25'><reportId>RP00000390</reportId><name>Fund Transfer</name><description>Fund Transfer</description></listdef>]') as "d",
      'RP00002004' as "r"
    returning content)
  as modified_definition
from dual;

MODIFIED_DEFINITION                                                                                                           
------------------------------------------------------------------------------------------------------------------------------
<listdef page="25"><reportId>RP00002004</reportId><name>Fund Transfer</name><description>Fund Transfer</description></listdef>
.

该函数用另一个文本字符串替换一个文本字符串,因此您可以更改

definition

如果要替换reportIf的任何值,而不仅仅是“RP00000390”,可以使用regexp\u replace:

该函数用另一个文本字符串替换一个文本字符串,因此您可以更改

definition

如果要替换reportIf的任何值,而不仅仅是“RP00000390”,可以使用regexp\u replace:


标记您正在使用的DMB,XML操作函数各不相同。因此,长话短说,就是这一点:从test_template_report中选择definition,其中template_id='RP00001242',然后您想用rp000002004替换RP00000390?是的,因此定义是XML格式的,只需要更改。我只给出了一个例子,但有许多记录需要插入。标记您正在使用的DMB,XML操作函数各不相同。所以,长话短说,就是这一点:从test_template_report中选择定义,其中template_id='RP00001242',您想用RP000002004替换RP00000390吗?是的,因此,定义是XML格式的,只需要更改。我只给出了一个例子,但是有很多记录需要插入。
insert into test_report
     ( company_id
     , report_id
     , brch_code
     , definition
     , description
     , editable_flag
     , executable_flag
     , name
     , report_type )
select 2420
     , 'RP00002004'
     , '0001'
     , replace(tr.definition, '<reportId>RP00000390</reportId>', '<reportId>RP00002004</reportId>')
     , tr.description
     , tr.editable_flag
     , tr.executable_flag
     , tr.name
     , '01'
from   test_template_report tr
where  tr.template_id = 'RP00001242';
insert into test_report
     ( company_id
     , report_id
     , brch_code
     , definition
     , description
     , editable_flag
     , executable_flag
     , name
     , report_type )
select 2420
     , 'RP00002004'
     , '0001'
     , regexp_replace(definition,'<reportId>[^<]+</reportId>','<reportId>RP00002004</reportId>')
     , tr.description
     , tr.editable_flag
     , tr.executable_flag
     , tr.name
     , '01'
from   test_template_report tr
where  tr.template_id = 'RP00001242';