使用 += 连接字符串

use*_*836 1 sql oracle

我正在尝试连接 sql 中的一些字符串。我想做的是

string organType = null;
if (liver!=null)  { organType += "LI, "; }
if (kidney !=null) { organType += "KI, "; }
if (intestine != null) { organType += "Intestine"; } 
...
Run Code Online (Sandbox Code Playgroud)

最终结果应该是organType = LI, KI, Intestine;

到目前为止,这是我的代码

创建或替换 PROCEDURE "insertDonInfo"(donNum IN NUMBER, offerDate IN DATE)

organType varchar2(100);
BEGIN

  select case when liver is not null then 'LI' 
              when kidney_r is not null then 'KR'
              when kidney_l is not null then 'KL' 
              when heart is not null then 'HE'
              when liver_domino is not null then 'LI-Dom'
              when lung_r is not null then 'LungR'
              when pancreas is not null then 'PA'
              when liver_split is not null then 'Lsplit'
              when lung_l is not null then 'LungL'
              when intestine is not null then 'Intestine' 
         end                         
from donors
where id = donNum;
Run Code Online (Sandbox Code Playgroud)

...

- - - - - - - - - - - - -更新 - - - - - - - - - - -

如何在 SQL 中将organType 连接为organType=LI, KR, KL, HE, ...;

Koe*_*rie 6

sql 没有 += 运算符。您必须逐列检查并连接。尝试了您的数据结构。

create table so_test (id number primary key, don_name varchar2(100), liver varchar2(1), heart varchar2(1), kidney_r varchar2(1));

insert into so_test (id, don_name, liver, heart, kidney_r) values (1, 'John','Y',NULL,'Y');
insert into so_test (id, don_name, liver, heart, kidney_r) values (2, 'Kathy',NULL,'Y','Y');

SELECT 
  don_name,
  RTRIM(
    CASE WHEN liver IS NOT NULL THEN 'LI, ' ELSE NULL END ||
    CASE WHEN heart IS NOT NULL THEN 'HE, ' ELSE NULL END ||
    CASE WHEN kidney_r IS NOT NULL THEN 'KR, ' ELSE NULL END
  ,', ') as organs
  FROM so_test;
Run Code Online (Sandbox Code Playgroud)

返回

John    LI, KR
Kathy   HE, KR
Run Code Online (Sandbox Code Playgroud)