Sql两种存储过程(单排序,多排序)

单条件单排序

ALTER PROCEDURE [dbo].[Universal_Infolist]
@tablename nvarchar(50),
@wherestr nvarchar(1000),
@oid  nvarchar(20),
@oway nvarchar(50),
@pagesize int = 10,
@pageindex int=1,
@recordcount int =0 output
AS
 declare @sqlstr nvarchar(1500)
 if(@pageindex=1)
 begin
 set @sqlstr = '  select  top '+Convert(nvarchar(20),@pagesize)+'  *  from  '+@tablename+' where 1=1 '+@wherestr+' order by '+@oid+' '+@oway+'  '
 end
 else
 begin
 set @sqlstr = '  select  top '+Convert(nvarchar(20),@pagesize)+'  *  from  '+@tablename+'  where 1=1 '+@wherestr+' and '+@oid+' <(select min('+@oid+' ) from(select top  '+convert(nvarchar(20), (@pageindex -1)*@pagesize)+ ' '+@oid+'  from '+@tablename+'  where 1=1 '+@wherestr+'  order by '+@oid+' '+@oway+' ) as O )  order by '+@oid+' '+@oway+'  '
 end
 Exec  sp_executesql @sqlstr
 --print @sqlstr;
 set @sqlstr =' select count('+@oid+') from '+@tablename+' where 1=1 '+@wherestr+'   '
 exec sp_executesql @sqlstr

---------------------------------------------------

多条件多种排序

ALTER PROCEDURE [dbo].[Universal_Infolist_1]

@tablename nvarchar(50),
@wherestr nvarchar(1000),
@oid  nvarchar(20),
@oway nvarchar(100),
@pagesize int = 10,
@pageindex int=1,
@recordcount int =0 output
AS
 declare @sqlstr nvarchar(1500)
 if(@pageindex=1)
 begin
 set @sqlstr = '  select  top '+Convert(nvarchar(20),@pagesize)+'  *  from  '+@tablename+' where 1=1 '+@wherestr+' order by '+@oway+' '
 end
 else
 begin
 set @sqlstr = '  select  top '+Convert(nvarchar(20),@pagesize)+'  *  from  '+@tablename+'  where '+@oid+' not in (select top '+convert(nvarchar(20), (@pageindex -1)*@pagesize)+ ' '+@oid+' from '+@tablename+' where 1=1 '+@wherestr+'  order by '+@oway+') '+@wherestr+' order by '+@oway+' '
 end
 Exec  sp_executesql @sqlstr
 --print @sqlstr;
 set @sqlstr = ' select count('+@oid+') from '+@tablename+' where 1=1 '+@wherestr+' '
 exec sp_executesql @sqlstr

转载于:https://www.cnblogs.com/lilei107/archive/2011/07/26/2117046.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值