我正在尝试编写一个 Oracle SQL 查询以返回一组当前配置为执行工作的值。
我有两张桌子:
VALUES
A
B
C
D
JOB, STRINGS_TO_SEARCH
1 {r, e, 'Hello' A w: B, xyz}
2 {ok, D }
每个作业的配置都存储在一个字符串中。我将如何编写一个只返回为作业配置的值的查询?
这甚至可以使用内置的 Oracle 函数吗?好像我可能要求生成一个动态查询......
create table T_VALUES (
VAL varchar2(20)
);
insert into T_VALUES values ('A');
insert into T_VALUES values ('B');
insert into T_VALUES values ('C');
insert into T_VALUES values ('D');
create table T_JOBS (
JOB_ID int,
STR_LIST varchar2(50)
);
insert into T_JOBS values (1, 'r, e, ''Hello'' A w: B, xyz');
insert into T_JOBS values (2, 'ok, D ');
select
job_id,
val
from
T_VALUES
natural join (
select
job_id,
regexp_substr(str_list, '[^ ,]+', 1, occ) as val
from
T_JOBS
cross join (
select level as occ from dual
connect by level <= (select max(length(str_list)) from T_JOBS)
)
)
order by 1, 2
编辑 :
改进版(使用 Ben 的想法)
create table T_VALUES (
VAL varchar2(20)
);
insert into T_VALUES values ('A');
insert into T_VALUES values ('B');
insert into T_VALUES values ('C');
insert into T_VALUES values ('D');
create table T_JOBS (
JOB_ID int,
STR_LIST varchar2(50)
);
insert into T_JOBS values (1, 'junk, values=A:NotA:B:r:e, more_junk');
insert into T_JOBS values (2, 'ok, values=D ');
create type nt_str as table of varchar2(50);
select
j.job_id,
v.val
from
T_JOBS j,
table(
cast(
multiset(
select
regexp_substr(
regexp_substr(j.str_list, 'values=([^, ]+)', 1, 1, 'i', 1),
'[^:]+', 1, level)
from dual
connect by
regexp_substr(
regexp_substr(j.str_list, 'values=([^, ]+)', 1, 1, 'i', 1),
'[^:]+', 1, level) is not null
) as nt_str
)
) t
join T_VALUES v
on v.val = t.column_value
order by 1, 2