select t.CatID , t.parentID  , case when exists(select 1 from tb where parentID = t.catid) then 'Y' else 'N' end from tb t

解决方案 »

  1.   

    select catid,parentid,[childcount] = select count(*) from table B where B.catid = A.parentid from table A
      

  2.   

    alter table tb add  ifsubid int default 0;
    goupdate t 
    set t.ifsubid=1
    from tb t
    where exists(select 1 from tb where parentID=t.CatID)
      

  3.   

    create table tb(CatID int, parentID int)
    insert into tb values(1 ,0)
    insert into tb values(2 ,0)
    insert into tb values(3 ,0)
    insert into tb values(5 ,0)
    insert into tb values(6 ,1)
    insert into tb values(7 ,3)
    insert into tb values(8 ,3)
    goselect t.CatID , t.parentID  , 
           case when exists(select 1 from tb where parentID = t.catid) then 'Y' else 'N' end ,
           (select count(1) from tb where parentID = t.catid)
    from tb tdrop table tb/*CatID       parentID                     
    ----------- ----------- ---- ----------- 
    1           0           Y    1
    2           0           N    0
    3           0           Y    2
    5           0           N    0
    6           1           N    0
    7           3           N    0
    8           3           N    0(所影响的行数为 7 行)
    */
      

  4.   

    select catid,parentid,[childcount] = select count(*) from table B where A.catid = B.parentid from table A
      

  5.   

    select *,'count'=(select count(flowID) from 表 as table2 where table1.CatID = table2.ParentID ) from 表 as table1 
      

  6.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([CatID] int,[parentID] int,[catname] varchar(14),[catdesc] int,[isenable] int,[seq] int,[islink] int,[linkurl] varchar(5))
    insert [tb]
    select 1,0,'政府信息公开35',3335,1,135,0,'33355' union all
    select 2,0,'信息公开目录',null,1,2,null,null union all
    select 3,0,'近期公开信息',1,3,0,null,null union all
    select 5,0,'最后一次测试',null,1,4,null,null union all
    select 6,1,'测试父77',6677,0,6677,0,'6777' union all
    select 7,3,'测试子专题',10,1,10,1,'aaaa' union all
    select 8,3,'子专题2',22333,1,233,1,'2222'
    alter table tb add  ifsubid int default 0;
    goupdate tb set ifsubid=0
    goupdate t 
    set t.ifsubid=1
    from tb t
    where exists(select 1 from tb where parentID=t.CatID)
    select * from tb/**CatID       parentID    catname        catdesc     isenable    seq         islink      linkurl ifsubid     
    ----------- ----------- -------------- ----------- ----------- ----------- ----------- ------- ----------- 
    1           0           政府信息公开35       3335        1           135         0           33355   1
    2           0           信息公开目录         NULL        1           2           NULL        NULL    0
    3           0           近期公开信息         1           3           0           NULL        NULL    1
    5           0           最后一次测试         NULL        1           4           NULL        NULL    0
    6           1           测试父77          6677        0           6677        0           6777    0
    7           3           测试子专题          10          1           10          1           aaaa    0
    8           3           子专题2           22333       1           233         1           2222    0(所影响的行数为 7 行)
    **/