分页存储过程代码

感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!

代码如下:


/* 
*@curentpage 当前页 
*@pagesize 每页记录数 
*@TableName 表名 
*@key 主键(自动排序) 
*@where 查询条件 
1)空为 null 
2)有查询条件不要带where 
*@order '0'表示 desc '1'是asc 
*@pageCount 总页数 
*/ 
create procedure Page 
@currentpage int,@pagesize int,@TableName varchar(30),@key varchar(30),@where varchar(50),@order varchar(1),@pageCount int,@str varchar(450) output 
as 
begin 
---------------执行的sql语句------------ 
declare @sql nvarchar(400),@ordreby nvarchar(200) 
declare @tempsql1 varchar(200),@tempsql2 varchar(200) 
---------------记录总数----------------- 
declare @count int 
---------------临时变量------------------------ 
declare @temp1 int,@temp2 int 

set @TableName=' '+@TableName+' ' 
set @key=' '+@key+' ' 

if @order='0' 
set @ordreby=' order by '+@key+'desc' 
else 
set @ordreby=' order by '+@key 

if @where='null' 
set @sql='select @count = count(*) from '+ @TableName 
else 
set @sql='select @count = count(*) from '+ @TableName+' where '+@where 

------------@count 付值(声明变量@count 在说明是output 内型)--------------------------- 
exec sp_executesql @sql,N'@count int out',@count out 
------------求总页数------------------------------ 
if (@count%@pagesize)=0 
set @pagecount=@count/@pagesize 
else 
set @pagecount=@count/@pagesize+1 
-----------判断显示当前页是否异常------------------ 
if @currentpage>@pagecount 
set @currentpage=@pagecount 
if @currentpage<1 
set @currentpage=1 
----------记录数小于页面显示记录数----------------- 
if(@currentpage=1) 
begin 
if @where='null' 
set @where=' ' 
else 
set @where=' where '+@where 
set @sql = 'select top'+ str(@pagesize)+' * from '+@TableName+@where+@ordreby 
end 
else 
begin 
/**//* ---------------desc---------------------- 
*@temp1表示前面的记录 
*@temp2表示后面的记录 
*假设一共77个记录,每次取10个。取67~58(第2页),去掉前面的57(1~57)个和后面的10个(77~66) 
*/ 
if @order=0 
begin 
set @temp1 = @count-@currentpage*@pagesize 
if @temp1<0 
set @temp1=0 
set @temp2 = (@currentpage - 1)*@pagesize 
if @where='null' 
begin 
set @tempsql1='select top ' + str(@temp1)+' '+@key+' from ' + @TableName+' order by ' +@key 
set @tempsql2='select top ' + str(@temp2)+' '+@key+' from ' + @TableName + @ordreby 
end 
else 
begin 
set @tempsql1='select top ' + str(@temp1)+' '+@key+' from ' + @TableName+' where '+@where+' order by ' +@key 
set @tempsql2='select top ' + str(@temp2)+' '+@key+' from ' + @TableName+' where '+@where+@ordreby 
end 
set @sql=' select top ' + str(@pagesize) + ' * from ' + @TableName + ' where '+@key+ ' not in ' 
set @sql= @sql+' ( '+ @tempsql1 +' ) and ' 
set @sql= @sql+@key+ ' not in ( '+@tempsql2 +' ) ' 
if @where='null' 
set @sql= @sql+@ordreby 
else 
set @sql= @sql+' and '+@where+@ordreby 
end 
/**//* ----------------asc--------------------- 
* @temp 表示前面显示的记录总数 
* 去掉 @temp 在取出 pagesize 个即可 
*/ 
else 
begin 
set @temp1=(@currentpage-1)*@pagesize 
if @where='null' 
set @tempsql1='select top '+ str(@temp1)+' '+@key+' from ' + @TableName + @ordreby 
else 
set @tempsql1='select top '+ str(@temp1)+' '+@key+' from ' + @TableName ++' where '+@where+@ordreby 
set @sql=' select top ' + str(@pagesize) + ' * from ' + @TableName + ' where '+@key+ ' not in ' 
set @sql=@sql+' ( '+@tempsql1+' ) ' 
if @where='null' 
set @sql= @sql+@ordreby 
else 
set @sql= @sql+' and '+@where+@ordreby 

end 
/**//* -------------------------------------*/ 
end 
set @str=@sql 
--exec sp_executesql @sql 

end 

GO 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_no=&#39;LJCG001H&#39; THEN dbo.ELTPNAME(a.fw_nu) ELSE d.fm_name END),e.fw_state_nm,f.fw_rmk_nm
if not exists(select name from syscolumns where name=&#39;tod_no&#39; and id=object_id(&#39;iebo09d12&#39;)) alter table iebo09d12 add tod_no varchar(
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_nm,g.fa_name from LJSS007H a (nolock) Left join LJPA002H b (nolock) On a.pa_no =b.pa_no Left jo
要在 SQL Server 2019 中设置定时自动重启,可以使用 Windows 任务计划程序。下面是详细的步骤: 步骤一:创建批处理文件 打开记事本。 输入以下内容: net stop &quot;SQL Server (MSSQLSERVER)&quot; net start &quot;SQ
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的事务日志已满,导致数据库操作失败。要解决这个问题,可以按照以下步骤操作: 1. 备份事务日志首先,备份事务日志以释放空间: BACKUP LOG [EastRiver] TO DISK = N&#39;C:\Backup\East
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标识ID,若不知道怎么查询数据库的标识ID, 打开SQL Server management studio,点击工具。选择SQL Server Profiler。 登录,登录成功后,如果有个默认弹窗,先取消 新建追踪 命名
--最新的解决方法 --先创建用户帐户,不进行授权,然后通过下面的SQL语句将该用户帐户关联至对应的数据库用户。优点是避免了重新授权的操作。 USE tempdbEXEC sp_change_users_login &#39;Update_One&#39;, &#39;iemis&#39;, &#3
命令: ALTER TABLE 表名 add 列名 数据类型 default 默认值 not null 例如: ALTER TABLE LJEL005H add el_req int default 15 not null
declare @i int set @i=340 while @i&lt;415 begin set @i=@iʱ insert into LJWK007H select &#39;2024&#39;,&#39;28&#39;,&#39;9110&#39;,&#39;3PTSD621000000
alter table LJSU002H add default (0) for su_totalamt with values
命令: 有默认值时用:alter table 表名 DROP 约束 alter table 表名 DROP COLUMN 列名 例如: alter table LJEL005H DROP COLUMN el_req 注意: 如果有默认值先删除约束,否则报错: --消息 5074,级别 16,状态 1
mysql报错Unknown collation: utf8mb4_0900_ai_ci 解决方案: 将文件内的所有 utf8mb4_0900_ai_ci 换成 utf8_general_ci utf8mb4 换成 utf8
SQL SERVER根据数据表的某个栏位查询另一个数据表符合条件的某个栏位的值,如果多行则合并为一张字符串形式 要在 SQL Server 中根据一个数据表的某个列查询另一个数据表符合条件的某个列的值,并将多行结果合并为一个字符串,可以使用 FOR XML PATH 子句来执行此操作。以下是一个示例
ALTER TABLE LJPA001H DROP CONSTRAINT DF_LJPA001H_pa_sex_1 ALTER TABLE LJPA001H ALTER COLUMN pa_sex VARCHAR(1)
DATEDIFF和DATEADD函数。DATEDIFF函数计算两个日期之间的小时、天、周、月、年等时间间隔总数。DATEADD函数计算一个日期通过给时间间隔加减来获得一个新的日期。要了解更多的DATEDIFF和DATEADD函数以及时间间隔可以阅读微软联机帮助。使用DATEDIFF和DATEADD函
select top 100 substring(qr_code,8,8) ,* from [LiuJun_PKh_lcfc_hf] --where right(ri,8) or substring(qr_code,8,8)=. select top 10 left(right(one_code,1
1、发现事务日志备份突然停止了 2、查看维护计划中的事务日志设置 3、发现备份任务中,事务日志需要指向的数据库不在 4、进入数据库属性 5、在选项中将恢复模式改为“完整”
select DB_ID(&#39;SBTERPDB&#39;)
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的事务日志已满,导致数据库操作失败。要解决这个问题,可以按照以下步骤操作: 1. 备份事务日志首先,备份事务日志以释放空间: BACKUP LOG [EastRiver] TO DISK = N&#39;C:\Backup\East