有一张表A(姓名 , 考试结果) 记录如下
(AA , 1)
(BB , 2)
(CC , )      结果为NULL想建立一个视图把考试结果从数字转换成相应的中文,想得到记录如下
(AA , 通过)
(BB , 未通过)
(CC , 缺考)这张视图怎么建立?

解决方案 »

  1.   

    create view v_test
    as
     select 姓名,考试结果=case when 考试结果 is null then '缺考'
                             when 考试结果=1 then '通过'
                             when 考试结果=2 then '未通过'
                             else '未知'
                         end
     from tb
    go
      

  2.   

    select 姓名, 考试结果 = case 考试结果 when 1 then '通过' when 2 then '未通过' else '缺考' end from 表A
      

  3.   

    create table asd 
    (aa varchar(10),bb int) insert asd values('AA',1) 
    insert asd values('BB',2) 
    insert asd values('CC',null) 
    select aa ,(CASE bb WHEN 1 THEN '通过' WHEN 2 THEN '未通过' else '缺考'  END) AS bb 
    from asd