日期:2014-05-17  浏览次数:20423 次

求一存储过程写法,TOP
A表有字段:USERID    NICKNAME
B表有字段:USERID    MONEY

要求查询 前15名  并且 判断B表是否已经重复USERID

谢谢

------解决方案--------------------
create proc aa
as
begin
select top 15 * into #a from A 
while exists(select 1 from #a)
begin
 declare @USERID=USERID from #a
 if not exists(select 1 from B where USERID=USERID )
   insert into B(..)select..from #a
 delete #a where USERID=USERID
end
end
go
------解决方案--------------------


declare @tab table(userid int,nickname varchar(20))
insert into @tab select top 15 * from a order by GETDATE()
insert into b select sel.userid,0 from(
select b.userid from b right join @tab on a.userid=@tab.userid where b.userid is null
)as sel

这个应该可以满足你的要求了。