当前位置: 代码迷 >> ASP.NET >> 这样存储过程该如何实现
  详细解决方案

这样存储过程该如何实现

热度:6463   发布时间:2013-02-26 00:00:00.0
这样存储过程该怎么实现?
现在有数据表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               匿名

请问,这样的一个存储过程该怎么写?

------解决方案--------------------------------------------------------
select b.topic, (case a.name when null then 'a.name ' else a.name end ) as NAME

FROM B表 b
LEFT JOIN
A表 a
on a.uid = b.uid
------解决方案--------------------------------------------------------
select b.topic, ISNULL(a.name, '匿名 ') from B表 b LEFT JOIN A表 a ON a.uid = b.uid

------解决方案--------------------------------------------------------
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 表B

end

  相关解决方案