这样存储过程该怎么实现?
现在有数据表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