现在有数据表A和B:A表                             B表
---------------------------     --------------------------
uid      name                    topic      uid
1       David                    xxxxx       1
2       Jack                      xxx        3
3       Mike                     xxxxxx      0经过存储过程处理后,得到这样一个表C:
C表
-------------------------
topic        name
xxxxx        David
xxx          Mike
xxxxxx       匿名请问,这样的一个存储过程该怎么写?

解决方案 »

  1.   

    select b.topic, (case a.name when null then 'a.name' else a.name end ) as NAMEFROM B表 b
    LEFT JOIN 
    A表 a
    on a.uid = b.uid
      

  2.   

    SQL :
    select b.topic, (case a.name when null then '匿名' else a.name end ) as NAME
    FROM B表 b
    LEFT JOIN 
    A表 a
    on a.uid = b.uid
      

  3.   

    存储过程:CREATE PROCEDURE  Getname
    ASbegin
    select b.topic, (case a.name when null then '匿名' else a.name end ) as NAME
    FROM B表 b
    LEFT JOIN 
    A表 a
    on a.uid = b.uid
    end
    GO
      

  4.   

    呵呵,楼上一个语句就完成了,还用存储过程?
    不过问一句,左外连接LEFT outer JOIN 的outer可以省略吗?从来没敢省过。
      

  5.   

    select b.topic, ISNULL(a.name, '匿名') from B表 b LEFT JOIN A表 a ON a.uid = b.uid
      

  6.   

    真快啊,竟落后了
    楼上的, outer 是可以省略的
      

  7.   

    create procedure P_test
    as
    begin
        select topic,uname = case when (select [NAME] from  表A where 表A.uid=表B.uid)  is null then '匿名' else (select [NAME] from  表A where 表A.uid=表B.uid) end  from 表Bend
      

  8.   

    谢谢楼上各位,已经结贴,bobomouse(波波)的语句可行。haonanernet(与时俱进) 的语句“匿名”两个字不能写到新的字段里,新字段仍旧显示null。
    不过还是要谢谢大家的帮助。