BookType   图书类型表BookTypeId   BookTypeName  flfatherID
1             马克思主义       0
2             自然科学    0
3             马克思著作    1
4             单行著作    3
5             单行著作精选     4              并且图书分类是无限延伸的。Book     图书信息表BookId  BookType   BookName     BookStock    
1       4           单行著作1      5
2       4           双行著作       5
3       3           专题汇编       5
根据传过来的flfatherID,查询BookTypeId、BookTypeName、BookStock,也就是图书类型ID、图书类型名称、该类图书的所有库存。
而我现在查的是,只要图书信息表中存在的BookTypeID,就能查出所有图书库存数量,而这个BookTypeID的flfatherID的图书库存数量却为0。哪位大侠能帮忙解决一下。

解决方案 »

  1.   

    tony 哥又开始了疯狂的强分行动
      

  2.   

    你需要先按照下面的函数获取到所有的子节点,然后再联合Book表进行统计./*
    标题:查询指定节点及其所有子节点的函数
    作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2008-05-12
    地点:广东深圳
    */create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
    insert into tb values('001' , null  , '广东省')
    insert into tb values('002' , '001' , '广州市')
    insert into tb values('003' , '001' , '深圳市')
    insert into tb values('004' , '002' , '天河区')
    insert into tb values('005' , '003' , '罗湖区')
    insert into tb values('006' , '003' , '福田区')
    insert into tb values('007' , '003' , '宝安区')
    insert into tb values('008' , '007' , '西乡镇')
    insert into tb values('009' , '007' , '龙华镇')
    insert into tb values('010' , '007' , '松岗镇')
    go--查询指定节点及其所有子节点的函数
    create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
    as
    begin
      declare @level int
      set @level = 1
      insert into @t_level select @id , @level
      while @@ROWCOUNT > 0
      begin
        set @level = @level + 1
        insert into @t_level select a.id , @level
        from tb a , @t_Level b
        where a.pid = b.id and b.level = @level - 1
      end
      return
    end
    go--调用函数查询001(广东省)及其所有子节点
    select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    002  001  广州市
    003  001  深圳市
    004  002  天河区
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 10 行)
    */--调用函数查询002(广州市)及其所有子节点
    select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    002  001  广州市
    004  002  天河区(所影响的行数为 2 行)
    */--调用函数查询003(深圳市)及其所有子节点
    select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    003  001  深圳市
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 7 行)
    */drop table tb
    drop function f_cid@@ROWCOUNT:返回受上一语句影响的行数。
    返回类型:integer。
    注释:任何不返回行的语句将这一变量设置为 0 ,如 IF 语句。
    示例:下面的示例执行 UPDATE 语句并用 @@ROWCOUNT 来检测是否有发生更改的行。UPDATE authors SET au_lname = 'Jones' WHERE au_id = '999-888-7777'
    IF @@ROWCOUNT = 0
       print 'Warning: No rows were updated'结果:(所影响的行数为 0 行)
    Warning: No rows were updated
      

  3.   

    大致为这样:
    --查询指定节点及其所有子节点的函数
    create function f_cid(@BookTypeId int) returns @t_level table(BookTypeId int , level int)
    as
    begin
      declare @level int
      set @level = 1
      insert into @t_level select @BookTypeId , @level
      while @@ROWCOUNT > 0
      begin
        set @level = @level + 1
        insert into @t_level select a.BookTypeId , @level
        from tb a , @t_Level b
        where a.flfatherID = b.BookTypeId and b.level = @level - 1
      end
      return
    end
    go--调用函数查询1及其所有子节点
    select m.* , n.* from
    (select a.* from tb a , f_cid(1) b where a.BookTypeId = b.BookTypeId order by a.BookTypeId) m,
    book n
    where m.BookTypeId = n.BookType如果还需要进行统计,你就sum(...) group by ....
      

  4.   

    --查询指定节点及其所有子节点的函数
    create function f_cid(@BookTypeId int) returns @t_level table(BookTypeId int , level int)
    as
    begin
      declare @level int
      set @level = 1
      insert into @t_level select @BookTypeId , @level
      while @@ROWCOUNT > 0
      begin
        set @level = @level + 1
        insert into @t_level select a.BookTypeId , @level
        from tb a , @t_Level b
        where a.flfatherID = b.BookTypeId and b.level = @level - 1
      end
      return
    end
    go--调用函数查询1及其所有子节点
    select m.* , n.* from
    (select a.* from tb a , f_cid(1) b where a.BookTypeId = b.BookTypeId order by a.BookTypeId) m,
    book n
    where m.BookTypeId = n.BookType执行出现这样的问题。消息 1033,级别 15,状态 1,第 2 行
    除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。麻烦给解释一下,我是个新手。
      

  5.   

    不好意思.你把order by 去掉.--调用函数查询1及其所有子节点 
    select m.* , n.* from 
    (select a.* from tb a , f_cid(1) b where a.BookTypeId = b.BookTypeId ) m, 
    book n 
    where m.BookTypeId = n.BookType 
      

  6.   

    create function f_cid(@BookTypeId int) returns @t_level table(BookTypeId int , level int)f_cid((@BookTypeId int)是图书类型ID吧!不是那个flfatherID吗?select m.BookTypeId,m.BookTypeName,m.flfatherID,isnull(sum(n.BookStock),0) AS BookStock from
    (select a.* from BookType a , f_cid(0) b where a.BookTypeId = b.BookTypeId) m,book n 
    where m.BookTypeId = n.BookType  GROUP BY m.BookTypeId,m.BookTypeName,m.flfatherID这个函数 f_cid(0) 都不能够查出  flfatherID为0的BookTypeId
      

  7.   

    create function f_cid(@BookTypeId int) returns @t_level table(BookTypeId int , level int)
    as
    begin
      declare @level int
      set @level = 1
      insert into @t_level select @BookTypeId , @level
      while @@ROWCOUNT > 0
      begin
        set @level = @level + 1
        insert into @t_level select a.BookTypeId , @level
        from BookType a , @t_Level b
        where a.flfatherID = b.BookTypeId and b.level = @level - 1
      end
      return
    end
    goselect m.BookTypeId,m.BookTypeName,m.flfatherID,isnull(sum(n.BookStock),0) AS BookStock from
    (select a.* from BookType a , f_cid(0) b where a.BookTypeId = b.BookTypeId) m,book n 
    where m.BookTypeId = n.BookType  GROUP BY m.BookTypeId,m.BookTypeName,m.flfatherID这些不能够统计出flfatherID=0的所有BookTypeId 
      

  8.   

    create function f_cid(@BookTypeId int) returns @t_level table(BookTypeId int , level int) 
    as 
    begin 
      declare @level int 
      set @level = 1 
      insert into @t_level select @BookTypeId , @level 
      while @@ROWCOUNT > 0 
      begin 
        set @level = @level + 1 
        insert into @t_level select a.BookTypeId , @level 
        from BookType a , @t_Level b 
        where a.flfatherID = b.BookTypeId and b.level = @level - 1 
      end 
      return 
    end 
    go select m.BookTypeId,m.BookTypeName,m.flfatherID,isnull(sum(n.BookStock),0) AS BookStock from 
    (select a.* from BookType a , f_cid(0) b where a.BookTypeId = b.BookTypeId) m,book n 
    where m.BookTypeId = n.BookType  GROUP BY m.BookTypeId,m.BookTypeName,m.flfatherID 这些不能够统计出flfatherID=0的所有BookTypeId