查询前几条的四种方法
<!--第一种-->
select top(10)* from AdminInfo where Id not in(select top(10) Id from AdminInfo)
<!--第二种-->
select top(10) * from AdminInfo where Id>(select MAX(Id)from AdminInfo where Id in (select top(10)Id from AdminInfo))
<!--第三种-->
select * from AdminInfo where Id between 1 and 20
<!--第四种-->
select *from(select *,ROW_NUMBER()over(order by id)as number from AdminInfo)t where t.number between 11 and 20
存储过程
循环存储过程:
create proc Prceduer(@count int)as
declare @countq int;
set @countq=@count;
while(@countq>0)
begin
print @countq;
set @countq=@countq-1;
End
调用:exec pagelistproc 10
exec 存储过程名 参数值
查询前几条信息的存储过程:
create proc pagelistproc (@pageIndex int,
@pagesize int,@tableName varchar(200),@columnName varchar(500),@orderby varchar(50),@sort varchar(50)
)as
declare @sql nvarchar(2000);
set @sql='select '+@columnName+' from(select '+@columnName+' ,ROW_NUMBER()over(order by '+@orderby+' )as number from '+@tableName+' )t where t.number between '+cast(((@pageIndex-1)*@pagesize)as varchar(200))+' and '+ cast((@pagesize*@pageIndex) as varchar(200))
exec(@sql)
调用:exec pagelistproc 1,10,'dbo.AdminInfo','*','Id','desc'