我想根据条件插入一条记录。如果列值包含括号,那么它应该插入一条记录,否则插入取决于
I want to insert a record based on the condition. If column value contains bracket then it should insert one record otherwise insert depending upon
CREATE TABLE new_staging(
e_id NUMBER(10),
e_owner VARCHAR2(255)
CONSTRAINT pk_new_staging PRIMARY KEY
( E_ID ) );
insert into new_staging values(1,'AUZA, PAUL OSA');
insert into new_staging values(2,'PAUL, REXX OSA');
insert into new_staging values(3,'PAUL, REXX OSA (PR56789)');
insert into new_staging values(4,'PAUL, REXX, OSA');
CREATE SEQUENCE new_target_sq;
CREATE TABLE new_target(
t_id NUMBER(10) DEFAULT new_target_sq.NEXTVAL NOT NULL,
e_id NUMBER(10),
t_owner VARCHAR2(30),
sort_order number(30),
CONSTRAINT pk_new_target PRIMARY KEY
( T_ID ) , CONSTRAINT FK_NEW_TARGET_E_ID FOREIGN KEY ( E_ID ) REFERENCES NEW_STAGING ( E_ID ));
我想根据 select 查询中的以下条件将值插入到 new_target table 中。目前,我正在验证 e_owner 是否包含逗号。如果它包含那么它是 selecting 那么多行。假设,e_id 包含 AUZA、PAUL OSA,那么它应该在 new_target table 中插入两条记录,对于三个逗号也是如此。但是,如果该列在 e_owner 列中有方括号 (),那么它应该只插入一条记录,例如 e_id 3
下面是 select 查询,它仅执行逗号分隔的工作,但还需要包含处理“(”的逻辑。
select e_id
,trim(regexp_substr(e_owner, '[^,]+', 1, level)) as owner,
level as sort_order
from new_staging
connect by e_id = prior e_id
and level <= regexp_count(e_owner, ',') + 1
and prior sys_guid() is not null
此外,在插入记录时它抛出错误。
insert into new_target(t_id,e_id,t_owner,sort_order)
(t_id.nextval,select e_id
,trim(regexp_substr(e_owner, '[^,]+', 1, level)) as owner
,level as sort_order
from new_staging
connect by e_id = prior e_id
and level <= regexp_count(e_owner, ',') + 1
and prior sys_guid() is not null);
有一次,我构建了处理括号的逻辑,然后我想将整个东西插入 new_target table。
insert into new_target(t_id, e_id, t_owner, sort_order)
select NEW_TARGET_SQ.nextval,e_id
,case when e_owner like '%(%' then e_owner else trim(regexp_substr(e_owner, '[^,]+', 1, level)) end,
level
from new_staging
connect by e_id = prior e_id
and level <= regexp_count(case when e_owner like '%(%' then 'x' else e_owner end, ',') + 1
and prior sys_guid() is not null
CREATE TABLE new_staging(
e_id NUMBER(10),
e_owner VARCHAR2(255)
CONSTRAINT pk_new_staging PRIMARY KEY
( E_ID ) );
insert into new_staging values(1,'AUZA, PAUL OSA');
insert into new_staging values(2,'PAUL, REXX OSA');
insert into new_staging values(3,'PAUL, REXX OSA (PR56789)');
insert into new_staging values(4,'PAUL, REXX, OSA');
CREATE SEQUENCE new_target_sq;
CREATE TABLE new_target(
t_id NUMBER(10) DEFAULT new_target_sq.NEXTVAL NOT NULL,
e_id NUMBER(10),
t_owner VARCHAR2(30),
sort_order number(30),
CONSTRAINT pk_new_target PRIMARY KEY
( T_ID ) , CONSTRAINT FK_NEW_TARGET_E_ID FOREIGN KEY ( E_ID ) REFERENCES NEW_STAGING ( E_ID ));
我想根据 select 查询中的以下条件将值插入到 new_target table 中。目前,我正在验证 e_owner 是否包含逗号。如果它包含那么它是 selecting 那么多行。假设,e_id 包含 AUZA、PAUL OSA,那么它应该在 new_target table 中插入两条记录,对于三个逗号也是如此。但是,如果该列在 e_owner 列中有方括号 (),那么它应该只插入一条记录,例如 e_id 3
下面是 select 查询,它仅执行逗号分隔的工作,但还需要包含处理“(”的逻辑。
select e_id
,trim(regexp_substr(e_owner, '[^,]+', 1, level)) as owner,
level as sort_order
from new_staging
connect by e_id = prior e_id
and level <= regexp_count(e_owner, ',') + 1
and prior sys_guid() is not null
此外,在插入记录时它抛出错误。
insert into new_target(t_id,e_id,t_owner,sort_order)
(t_id.nextval,select e_id
,trim(regexp_substr(e_owner, '[^,]+', 1, level)) as owner
,level as sort_order
from new_staging
connect by e_id = prior e_id
and level <= regexp_count(e_owner, ',') + 1
and prior sys_guid() is not null);
有一次,我构建了处理括号的逻辑,然后我想将整个东西插入 new_target table。
insert into new_target(t_id, e_id, t_owner, sort_order)
select NEW_TARGET_SQ.nextval,e_id
,case when e_owner like '%(%' then e_owner else trim(regexp_substr(e_owner, '[^,]+', 1, level)) end,
level
from new_staging
connect by e_id = prior e_id
and level <= regexp_count(case when e_owner like '%(%' then 'x' else e_owner end, ',') + 1
and prior sys_guid() is not null