根据条件将值连接到 SELECT

Concatenate value into SELECT on a condition

我需要在 table 中插入一个连接值列表,但如果 p_is_broker 变量为真,则需要添加第一个值。为简单起见,假设这是真的。

insert into exports (session_id, row_sequence, row_data)
select token,
       1, 'Manager'
          ||','||'Company'
          ||','||'Driver Name'
          ||','||'Registration'
          ||','||'Vehicle'
from dual
union
select token,
       rownum + 1,
       '"'||replace(manager,'"','""')
          ||'","'||replace(company,'"','""')
          ||'","'||replace(driver_name,'"','""')
          ||'","'||replace(registration,'"','""')
from table(cast (p_data as data_t));

如果 p_is_broker 为真,我如何插入 'Manager'?

我试过类似的方法,但没有用。

 insert into exports (session_id, row_sequence, row_data)
 select token,
        1, (CASE WHEN p_is_broker THEN 'Manager' END)
            ||','||'Company'
            ||','||'Driver Name'
            ||','||'Registration'
            ||','||'Vehicle'
 from dual;

如果 'Manager' 存在,我们想要第一个 ||','||,这变得非常棘手。

将逗号移到大小写内:

insert into session_csv_Exports (session_id, row_sequence, row_data)
select
  token,
  1,
  (CASE WHEN p_is_broker = 'Y' THEN 'Manager,' ELSE '' END)
  ||'Company'
  ||','||'Driver Name'
  ||','||'Registration'
  ||','||'Vehicle'
from dual;

此外,我想不出任何连接文本文字的理由。您应该简化为:

insert into session_csv_Exports (session_id, row_sequence, row_data)
select
  token,
  1,
  (CASE WHEN p_is_broker THEN 'Manager,' ELSE '' END)
  || 'Company,Driver Name,Registration,Vehicle'
from dual;