易截截图软件、单文件、免安装、纯绿色、仅160KB

sql语句获取叶子节点

表结构 为
id parentid name
1    0
2    0
3    1
4    3
5    2
输入0现在想得到
4 5
也就是获取叶子节点,改怎么实现? 谢谢

http://blog.csdn.net/wufeng4552/archive/2009/09/30/4619995.aspx

我要的是叶子节点,就是最底层的节点

0 得到4,5 ?

引用
http://blog.csdn.net/wufeng4552/archive/2009/09/30/4619995.aspx

这个好

BOM 精华

对 0 得到 4 5

SQL code:
-->Title:Generating test data
-->Author:wufeng4552
-->Date :2009-09-30 14:27:56

if not object_id('tb') is null
drop table tb
Go
Create table tb([id] int,[parentid] int,[name] varchar(10))
Insert tb
select 1,0,null union all
select 2,0,null union all
select 3,1,null union all
select 4,3,null union all
select 5,2,null
Go
-->Title:查找指定節點下的子結點
if object_id('Uf_GetChildID')is not null drop function Uf_GetChildID
go
create function Uf_GetChildID(@ParentID int)
returns @t table(ID int)
as
begin
insert @t select ID from tb where ParentID=@ParentID
while @@rowcount<>0
begin
insert @t select a.ID from tb a inner join @t b
on a.ParentID=b.id and
not exists(select 1 from @t where id=a.id)


相关问答:

sql server 存储过程循环赋值的问题

小弟是个新手 现在有个问题一直不能解决
例如
procedure produce_proc
    @p001 nvarchar(8000),
    @p002 nvarchar(8000),
    @p003 nvarchar(8000),
  & ......

SQL server数据集出错求助

两种错误:
1.如果我这样申明:ResultSet rs;错误提示如下:

An error occurred at line: 51 in the jsp file: /index.jsp
The local variable rs may not have been initialized
48:    ......

sql查询疑问

表中按datadate,tradeid为主键,也就是说tradeid会重复出现在不同天中,同一天的tradeid是不同的。现在我希望取一段时间范围内不重复的tradeid,且只要每个tradeid的最后一次出现的那条。sql语句该怎么写

: ......

SQL语句效率问题 - 其他数据库开发 / 其他数据库

大家帮忙看看这2个sql语句哪个查询的速度更快点。谢谢帮忙。比较着急。在做性能测试。
select * from
  表A LEFT OUTER JOIN 表B ON (表A.id || ' ' =表B.id) ,表C , 表D, 表E
Where其他条件
select * ......

请教一个SQL查询

有如下两个表
tb1
col  col1
abc  111 
cde  111 
qaz  222   
tgb  222

tb2
col2  col3
abc    111&n ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号