我正在尝试在 sql 中连接一些字符串。我想做的是类似
string organType = null;
if (liver!=null) { organType += "LI, "; }
if (kidney !=null) { organType += "KI, "; }
if (intestine != null) { organType += "Intestine"; }
...
最终结果应该是organType = LI, KI, Intestine;
到目前为止,这是我的代码
创建或替换过程“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;
...
------------------------更新--------------------
如何在 SQL 中将 organType 连接为 organType=LI, KR, KL, HE, ...;
最佳答案
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;
返回
John LI, KR
Kathy HE, KR
关于sql - 使用 += 连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58656678/