SQL用分隔符VALUES分割 获得TEXT 问题补充
表A
ID VALUES
1 a,b,c
2 c,d
3 null
4 e
表B
ID text value
1 一 a
2 二 b
3 五 c
4 六 d
想写SQL 得
ID 字段A 字段B
1 a,b,c 一,二,五
2 c,d 五,六
3 null null
4 e null
请问怎么写
------解决方案--------------------
create table ta(id int ,val varchar(100))
insert into ta(id,val)
select 1,'a,b,c'
union all select 2 ,'c,d'
union all select 3,null
union all select 4,'e'
create table tb(id int ,[text] varchar(100),val varchar(100))
insert into tb(id,[text],val)
select 1,'一','a'
union all select 2 ,'二','b'
union all select 3,'五','c'
union all select 4,'六','d'
select * from ta
select * from tb
create function dbo.fn_name
(@v varchar(100))
returns varchar(100)
as
begin
declare @s varchar(1000)
set @s=''
select @s=@s+[text]+','
from tb
where charindex(val,@v)>0
if (len(@s)>0)
set @s= left(@s,len(@s)-1)
return @s
end
/*
drop function dbo.fn_name
drop table ta,tb
*/
select *,dbo.fn_name(isnull(val,'')) as val2
from ta
======================
1 a,b,c 一,二,五
2 c,d 五,六
3 NULL
4 e