zoukankan      html  css  js  c++  java
  • 分页存储过程

    ALTER PROCEDURE [dbo].[proc_Hotel_Pagedata] 
        @pageSize int, 
        @pageCurrent int,
        @province varchar(20),
        @city varchar(20),
        @brandId varchar(20)
    AS
    	declare @sql nvarchar(4000)
    BEGIN
    	select * from 
    	(select ROW_NUMBER() over(ORDER BY hotelId) RowNum, * from nbapisdk_Hotel where 
    	province = (CASE WHEN (@province IS NULL) THEN province ELSE @province END) 
    	and city = (CASE WHEN (@city IS NULL) THEN city ELSE @city END) 
    	and brandId = (CASE WHEN (@brandId IS NULL) THEN brandId ELSE @brandId END)
    	)OrderData 
    	where RowNum between (@pageCurrent - 1)*@pageSize + 1 and @pageCurrent * @pageSize order by hotelId
    	execute(@sql)
    END

    上面是正常的SQL语句,下面是拼接的分页存储过程

    ALTER PROCEDURE [dbo].[proc_tourol_B2COrder_Pagedata]
        @pageSize int, 
        @pageIndex int,
        @count int out,
        @sqlwhere nvarchar(200)
        
    AS
    declare @sql nvarchar(4000)
    declare @sql2 nvarchar(4000)
    BEGIN
    	set @sql=
    	'select * from 
    	(select ROW_NUMBER() over(ORDER BY Orderid) RowNum, * from tourol_B2COrder '+@sqlwhere+'
    	)OrderData 
    	where RowNum between '+CONVERT(nvarchar(100),@pageIndex)+'*'+CONVERT(nvarchar(100),@pageSize)
    	+' + 1 and ('+CONVERT(nvarchar(100),@pageIndex)+'+1) * '+CONVERT(nvarchar(100),@pageSize)
    	+' order by Orderid'
    	print @sql
    	exec (@sql)
    	
    	set @sql2=
    	'select @count=count(*) from 
    	(select ROW_NUMBER() over(ORDER BY Orderid) RowNum, * from tourol_B2COrder '+@sqlwhere+'
    	)OrderData'
    	print @sql2
    	exec sp_executesql @sql2,N'@count INT OUT',@count=@count OUT
    	print @count
    END

    这里需要注意的是传出参数的写法

  • 相关阅读:
    自定义ASP.NET MVC Html标签辅助方法
    解决github.com无法访问
    Func<>用法
    Entity Framework 在OrderBy排序中使用字符串
    <input type="file"> 标签详解
    JS实现上传图片的三种方法并实现预览图片功能
    一个关于双目运算符的测试
    堆和栈的区别
    C# 代码笔记_tuple元组
    小程序前后端自定义登录与一键登录兼容demo分享
  • 原文地址:https://www.cnblogs.com/TivonStone/p/2985175.html
Copyright © 2011-2022 走看看