zoukankan      html  css  js  c++  java
  • SQL 存储过程分页在网上找到代码,收藏起来

    --这句是执行语句
    exec  sp_PublicTurnPage 'student',20,2,'SID','DESC','*','',''
    
    --创建存储过程
    create
    proc sp_PublicTurnPage2( @TBName nvarchar(2000)='student',--表名,如 pinyin @PageSize int =10,--每页的记录数,默认为 10 @CurPage int =1,--表示当前页 1 @KeyField nvarchar(100) ='SID',--关键字段名,默认为 ID,该字段要求是表中的索引 或 无重复和不为空的字段 @KeyAscDesc nvarchar(4)='DESC',--关键字的升、降序,默认为升序 ASC , 降序为 DESC @Fields nvarchar(2000)='*',--所选择的列名,默认为全选 @Condition nvarchar(2000)='',--where 条件,默认为空 @Order nvarchar(200)=''--排序条件,默认为空 )as if @TBName = '' begin raiserror('student',11,1) return end if @PageSize <=0 or @CurPage <0 begin raiserror('当前页数和每页的记录数都必须大于零!',11,1) return end if @KeyAscDesc = 'DESC' set @KeyAscDesc = '<' else set @KeyAscDesc = '>' if @Condition <> '' set @Condition = ' where ' + @Condition declare @SQL nvarchar(2000) set @SQL = '' if @CurPage = 1 set @SQL = @SQL + 'SELECT Top ' + cast(@PageSize as nvarchar(20)) + ' ' + @Fields + ' FROM ' + @TBName + @Condition + ' ' + @Order else begin declare @iTopNum int set @iTopNum = @PageSize * (@CurPage - 1) set @SQL = @SQL + 'declare @sLastValue nvarchar(100)' + char(13) set @SQL = @SQL + 'SELECT Top ' + cast(@iTopNum as nvarchar(20)) + ' @sLastValue=' + @KeyField + ' FROM ' + @TBName + @Condition + ' ' + @Order + char(13) declare @Condition2 nvarchar(200) if @Condition = '' set @Condition2 = ' where ' + @KeyField + @KeyAscDesc + '@sLastValue ' else set @Condition2 = ' and ' + @KeyField + @KeyAscDesc + '@sLastValue ' set @SQL = @SQL + 'SELECT Top ' + cast(@PageSize as nvarchar(20)) + ' ' + @Fields + ' FROM ' + @TBName + @Condition + @Condition2 + @Order end EXECUTE sp_executesql @SQL
  • 相关阅读:
    Ubuntu操作系统如何设置默认启动内核
    设置和取消环境变量
    vue create与vue init的区别
    base64 编解码
    获取url中的参数
    ubuntu账户密码正确但是登录不了怎么办
    斐波那契数列的递推式-----动态规划
    Round-Robin轮询调度法及其实现原理
    MySql中float类型的字段的查询
    使用 Python 从网页中提取主要文本内容
  • 原文地址:https://www.cnblogs.com/xgxhellboy/p/SQL.html
Copyright © 2011-2022 走看看