sql 语句:根据父节点查找所有子节点
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER function f_id(@parentid int)
returns @re table(orderid int,parentid int,title nvarchar(50),level int)
as
begin
declare @l int
set @l=0
insert @re select @parentid,null,'',-1
insert @re select TabID, ParentTabID ,TabName,@l from rb_Tabs
where ParentTabID=@parentid
while @@rowcount>0
begin
set @l=@l+1
insert into @re select
a.TabID,a.ParentTabID,a.TabName,@l
from rb_Tabs a,@re b
where a.ParentTabID=b.orderid and b.level=@l-1
end
return
end
go
使用方法:
declare @cat int
set @cat = 406
SELECT
rb_Products_st.ProductID,
rb_Products_st.DisplayOrder,
rb_Products_st.ModelNumber,
rb_Products_st.ModelName,
rb_Products_st.UnitPrice,
rb_Products_st.FeaturedItem,
rb_Products_st.LongDescription,
rb_Products_st.ShortDescription,
rb_Products_st.MetadataXml,
rb_Products_st.Weight,
rb_Products_st.TaxRate
FROM
rb_Products_st
WHERE
rb_Products_st.CategoryID in (select orderid from dbo.f_id(@cat) )