|
发表于 2020-2-1 12:09:01
|
显示全部楼层
create table ta(IDX int, CODE varchar(50), DEPTCODE varchar(50), PARTNAME varchar(50))
insert ta select 1, 'CN00001', 'CN00001001' , 'PART1'
union all select 2, 'CN00001', 'CN00001002', 'PART2'
union all select 3, 'CN00001', 'CN00001001001', 'PART3'
union all select 4, 'CN00001', 'CN00001002001', 'PART4'
union all select 5, 'CN00002', 'CN00002001', 'PART5'
union all select 6, 'CN00002', 'CN00002001001', 'PART6'
create function test_f(@DEPTCODE varchar(50))
returns varchar(100)
as
begin
declare @s varchar(100)
select @s=isnull(@s,'')+DEPTCODE+':'+PARTNAME+'|' from ta where charindex(@DEPTCODE,DEPTCODE)=1
return @s
end
select IDX,CODE, 显示=dbo.test_f(DEPTCODE) from ta where len(dbo.test_f(DEPTCODE))>21
IDX CODE 显示
----------- -------------------------------------------------- ----------------------------------------------------------------------------------------------------
1 CN00001 CN00001001:PART1|CN00001001001:PART3|
2 CN00001 CN00001002:PART2|CN00001002001:PART4|
5 CN00002 CN00002001:PART5|CN00002001001:PART6|
(所影响的行数为 3 行)
|
|