表结构及数据
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
急呀,该怎样写呢
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
急呀,该怎样写呢
select id ,case when isnull(pid,'')='' then '0' else pid end
from A
create view vw_b
as
select id,pid=case when isnull(pid,'')='' then '0' else pid end from a
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 行)
select id ,case when isnull(pid,'')='' then '0' else pid end from A