create function getString(@account_no nvarchar(100))
returns nvarchar(200)
as
begin
declare @voucher_no nvarchar(50)
select @voucher_no=voucher_no from 表 where account_no=@account_no declare @s nvarchar(4000)
set @s=''
select @s=@s+','+account_no from 表 where voucher_no=@voucher_no and account_no<>@account_no
set @s=stuff(@s,1,1,'')
return (@s)
end
go
--调用
select account_no,voucher_no,yesno,dbo.getString(account_no) as OTHER_ACCOUNT from 表 where yesno=1drop function getString
--结果
account_no voucher_no yesno OTHER_ACCOUNT
10101 F000001 1 10202,204,205
10202 F000001 1 10101,204,205
10203 F000002 1 203,304(3 row(s) affected)
returns nvarchar(200)
as
begin
declare @voucher_no nvarchar(50)
select @voucher_no=voucher_no from 表 where account_no=@account_no declare @s nvarchar(4000)
set @s=''
select @s=@s+','+account_no from 表 where voucher_no=@voucher_no and account_no<>@account_no
set @s=stuff(@s,1,1,'')
return (@s)
end
go
--调用
select account_no,voucher_no,yesno,dbo.getString(account_no) as OTHER_ACCOUNT from 表 where yesno=1drop function getString
--结果
account_no voucher_no yesno OTHER_ACCOUNT
10101 F000001 1 10202,204,205
10202 F000001 1 10101,204,205
10203 F000002 1 203,304(3 row(s) affected)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货