查询根目录下的所有记录!


/*
SupTypeID代表 上级目录 0表示一级目录
SupTypeID=1表示为ID=1的子目录,依此类推,ID=8 是ID=6 的子目录
读取ID=1时把他所有的产品读出来
*/


if exists (select 1 from sysobjects where id=object_id('tt'))
 drop table tt
create table tt(ID int,TypeName varchar(50),SupTypeID int)
insert into tt
select '1',          'Agriculture', 0
union all select '2',          ' Apparel & Fashion', 0                               
union all select '3',          ' Automobile',0                                        
union all select '4',          ' Business Services',  0                               
union all select '5',          ' Agriculture & By-product Agents', 1                  
union all select '6',          'Agriculture Product Stocks',  1                     
union all select '7',          ' Agriculture Products Processing',  1                 
union all select '8',          ' Other', 6                                             
union all select '9',          ' Animal Extract',  6                                  
union all select '10',          'Animal  Fodders', 7                                    

select * from tt
/*
ID                    TypeName                                                        SupTypeID
---------------------------------------------------------------------------------------------
1 Agriculture 0
2  Apparel & Fashion                                                   0
3  Automobile                                                   0
4  Business Services                                                   0
5  Agriculture & By-product Agents                                 1
6 Agriculture Product Stocks                                 1
7  Agriculture Products Processing                                 1
8  Other                                                                      6
9  Animal Extract                                                    6
10 Animal  Fodders                                                    7
*/
drop function F_GetChlidren
go
create  function F_GetChlidren(@id int)
returns   @tree table (ID int,SupTypeID int)
 as
begin
insert @tree  select ID,SupTypeID from tt where ID=@id
while @@rowcount>0
insert  @tree
select  a.ID,a.SupTypeID from tt a inner join @tree b  on  a.SupTypeID=b.ID and  a.ID not in (select id from @tree )
return
end

go

select  a.*  from tt a, dbo.F_GetChlidren(1) b where a.ID=b.ID

/*
ID                    TypeName                                                        SupTypeID
---------------------------------------------------------------------------------------------
1 Agriculture                                                                      0
5  Agriculture & By-product Agents                                 1
6 Agriculture Product Stocks                                 1
7  Agriculture Products Processing                                 1
8  Other                                                                      6
9  Animal Extract                                                    6
10 Animal  Fodders                                                    7
*/

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值