日期:2014-05-19  浏览次数:20462 次

想写一个存储过程
取出A表中所有的username字段,查询每个username在B表中是否存在,构造(username,isexist)临时表C。
如A表中的user1在B表中的username字段存在,则插入(user1,yes)到C表,如A表中的user2在B表中username不存在,则插入(user2,no)到C表。
先谢了!

------解决方案--------------------
Insert C
Select Distinct A.username, (Case When B.username Is Null Then 'no ' Else 'yes ' End) As isexist
From A
Left Join B
On A.username = B.username
------解决方案--------------------
CREATE PROCEDURE a

AS
begin
select
username = (case when isnull(isexist) then 'user2 ' else 'user1 ' end),
isexist = (case when isnull(isexist) then 'no ' else 'yes ' end)
into #c
from (select a.username as username,b.username as isexist from a left join b on a.username = b.username)
select * from #c
end