表结构及数据
a :
id   pid
001  
002  001
003   
004  001
005  003
006  002
我想要得到的结果是当pid为空时,把它赋值为0即结果为
B:
id   pid
001  0
002  001
003  0
004  001
005  003
006  002
但是表B的结果不能用改变A的数据而得来,A表的记录是不变的,B变动态继承A,有点象视图的,但当pid为空时变为0
急呀,该怎样写呢

解决方案 »

  1.   

    ??
    select id ,case when isnull(pid,'')='' then '0' else pid end
    from A
      

  2.   

    SELECT ID,ISNULL(PID,0)PID FROM A
      

  3.   

    select id ,case when isnull(pid,'')='' then '0' else pid end from A
      

  4.   

    --那就用视图。
    create view vw_b 
    as
    select id,pid=case when isnull(pid,'')='' then '0' else pid end from a 
      

  5.   

    ---测试数据---
    if object_id('[a]') is not null drop table [a]
    go
    create table [a]([id] varchar(3),[pid] varchar(3))
    insert [a]
    select '001','' union all
    select '002','001' union all
    select '003','' union all
    select '004','001' union all
    select '005','003' union all
    select '006','002'
     
    ---查询---
    select 
      id,
      case when len(isnull(pid,''))=0 then '0' else pid end as pid
    from
      a---结果---
    id   pid  
    ---- ---- 
    001  0
    002  001
    003  0
    004  001
    005  003
    006  002(所影响的行数为 6 行)
      

  6.   


    select id ,case when isnull(pid,'')='' then '0' else pid end from A