------解决思路---------------------- declare @userid int declare @n int set @userid=1 set @n=10 select sum([count]) from ( select top(@n) * from tablename where userid= @userid and useFlag='0' ) as a
declare @userid int declare @n int set @userid=1 set @n=10 select sum([count]) from ( select top(@n) * from tablename where userid= @userid and useFlag='0' ) as a
试试这个 declare @userid int=1, @n int=1,@sum int,@sql varchar(max) begin tran ;with cte as ( select count,useflag from (select top (@n) * from tt where userid=@userid and useflag!=0 order by count) as t) select @sum=SUM(count) from cte print @sum update top (@n) tt set useflag =1 where userid=1 and useflag=0 end
declare @userid int declare @n int set @userid=1 set @n=10 select sum([count]) from ( select top(@n) * from tablename where userid= @userid and useFlag='0' order by ID ) as a update tablename set useFlag='1' where ID in (select top(@n) ID from tablename where userid= @userid and useFlag='0' order by ID)