写个函数create function fn_ids(
@id int
)
returns varchar(200)
as
begin
declare @t table (id int)
declare @r varchar(200)
set @r=''
insert @t select id from table1 where id=@id
while exists (select 1 from table1 where id not in (select id from @t)
and id_id in (select id from @t)
)
insert @t select id from table1 where id not in (select id from @t)
and id_id in (select id from @t)
select @r=@r+','+cast(id as varchar) from @t
if @r<>''
set @r=stuff(@r,1,1,'')
return @r
end
go--调用
select dbo.fn_ids(2)or:select dbo.fn_ids(id) as ids,* from table1
where name='董事会'
@id int
)
returns varchar(200)
as
begin
declare @t table (id int)
declare @r varchar(200)
set @r=''
insert @t select id from table1 where id=@id
while exists (select 1 from table1 where id not in (select id from @t)
and id_id in (select id from @t)
)
insert @t select id from table1 where id not in (select id from @t)
and id_id in (select id from @t)
select @r=@r+','+cast(id as varchar) from @t
if @r<>''
set @r=stuff(@r,1,1,'')
return @r
end
go--调用
select dbo.fn_ids(2)or:select dbo.fn_ids(id) as ids,* from table1
where name='董事会'
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货