求在一张表中循环取数据的SQL

HZBbinbin 2007-08-22 12:09:21
我有一张表的字段设计是
id Pid
1 0
2 1
3 2
4 2
5 3
其中id 代表代号
Pid 代表对应的父ID号
Pid为0的代表最顶,上面没有父ID号了
比如 id 为2的父ID为1

我现在想问下一:如何通过SQL语句 输入一个ID号,找出它的子ID号和它本生的ID号
比如:我输入 2 ,输出结果为 2,3,4,5
输入0,输出结果为0,1,2,3,4,5
...全文
927 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
HZBbinbin 2007-08-23
  • 打赏
  • 举报
回复
谢谢!好心人真多
brother2605 2007-08-22
  • 打赏
  • 举报
回复
动作真快啊
brother2605 2007-08-22
  • 打赏
  • 举报
回复
--试试这个
if exists(select name from sysobjects where name='test'and type='U')
drop table test
go
create table test(id int not null,pid int not null)
go
insert into test
select 1,0
union select 2 ,1
union select 3,2
union select 4,2
union select 5,3
go
if exists(select name from sysobjects where name='fn_test'and type='FN')
drop function fn_test
go
create function fn_test
(
@pID int
)
returns varchar(100)
as
begin
declare @return varchar(100);
set @return=cast(@pID as varchar(5));
with result(id,pid)
as
(
select id,pid from test where pid=@pID
union all
select b.id,b.pid from result a inner join test b on a.id=b.pid
)
select @return=@return+','+cast(id as varchar(5)) from result;
return @return
end
go

select [dbo].[fn_test](0)
select [dbo].[fn_test](1)
select [dbo].[fn_test](2)
/*结果
----------------------------------------------------------------------------------------------------
0,1,2,3,4,5

(1 row(s) affected)


----------------------------------------------------------------------------------------------------
1,2,3,4,5

(1 row(s) affected)


----------------------------------------------------------------------------------------------------
2,3,4,5

(1 row(s) affected)
*/
HZBbinbin 2007-08-22
  • 打赏
  • 举报
回复
还是高人多啊!现在把两个方法弄明白,,谢谢大家百忙之中,抽空帮我解决问题。。分不多。。非常感谢
HZBbinbin 2007-08-22
  • 打赏
  • 举报
回复
@@rowcount实在是太好了,学习啊
HZBbinbin 2007-08-22
  • 打赏
  • 举报
回复
消化当中,太厉害了,,,
zjexe 2007-08-22
  • 打赏
  • 举报
回复
楼上的好方法
chuifengde 2007-08-22
  • 打赏
  • 举报
回复
create table [Table](id int,pid int)
insert [Table] select 1, 0
union all select 2 ,1
union all select 3 ,2
union all select 4 ,2
union all select 5 ,3

go
create function fn_GetNode(@id int)
returns @a table(a int)
as
begin
declare @x table(a int)
declare @y table(a int)

insert @a select @id
insert @x select id from [Table] where pid=@id
insert @a select * from @x
while @@rowcount>0
begin
insert @a select id from [Table] where pid in(select * from @x)
insert @y select id from [Table] where pid in(select * from @x)
delete from @x
insert @x select * from @y
delete from @y
end
return
end
go
declare @y varchar(100)
select @y=isnull(@y+',','')+ltrim(a) from dbo.fn_getnode(0) order by a
select @y
zjexe 2007-08-22
  • 打赏
  • 举报
回复
create table tb(
id int not null,
pid int not null)
go
insert into tb select 1,0
union select 1,0
union select 2 ,1
union select 3,2
union select 4,2
union select 5,3
go
create function ge(@a int)
returns varchar(200) as
begin
declare @s varchar(200)
declare @i int
declare @p int
declare @cur cursor
set @s=Convert(varchar(20),@a)
select @i=count(*) from tb where pid=@a
if @i=0
return @s
set @p=@a
if @i>0
begin

--游标操作
set @cur=cursor LOCAL SCROLL for select id from tb where pid=@p
open @cur
Fetch next from @cur into @P
while @@FETCH_STATUS=0
BEGIN
SET @S=@S+','+DBO.GE(@P)
Fetch next from @cur into @P
END
close @cur
DEALLOCATE @cur

end
return @s
end
go
select dbo.ge(0)
select dbo.ge(2)
drop table tb
drop function ge

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧