sql分页存储过程简析
来源:未知 责任编辑:责任编辑 发表时间:2015-03-01 01:38 点击:次
sql分页存储过程简析
1.支持多列排序,自认为效率比较高的一个存储过程:
/****** 对象: StoredProcedure [dbo].[P_viewPage] 脚本日期: 05/14/2012 08:49:34 ******/ www.2cto.com
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROC [dbo].[P_viewPage]
-- Add the parameters for the stored procedure here
@TableName VARCHAR(200), --表名
@FieldList VARCHAR(2000), --显示列名,如果是全部字段则为*
@PrimaryKey VARCHAR(100), --单一主键或唯一值键
@Where VARCHAR(8000), --查询条件不含'where'字符,如id>10 and len(userid)>9
@Order VARCHAR(1000), --排序不含'order by'字符,如id asc,userid desc,必须指定asc或desc
--注意当@SortType=3时生效,记住一定要在最后加上主键,否则会让你比较郁闷
@SortType INT, --排序规则1:正序asc 2:倒序desc 3:多列排序方法
@RecorderCount INT, --记录总数0:会返回总记录
@PageSize INT, --每页输出的记录数
@PageIndex INT, --当前页数
@TotalCount INT OUTPUT, --记返回总记录
@TotalPageCount INT OUTPUT --返回总页数
AS www.2cto.com
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
IF ISNULL(@TotalCount,'') = '' SET @TotalCount = 0
SET @Order = RTRIM(LTRIM(@Order))
SET @PrimaryKey = RTRIM(LTRIM(@PrimaryKey))
SET @FieldList = REPLACE(RTRIM(LTRIM(@FieldList)),' ','')
WHILE CHARINDEX(', ',@Order) > 0 OR CHARINDEX(' ,',@Order) > 0
BEGIN
SET @Order = REPLACE(@Order,', ',',')
SET @Order = REPLACE(@Order,' ,',',')
END
IF ISNULL(@TableName,'') = '' OR ISNULL(@FieldList,'') = ''
OR ISNULL(@PrimaryKey,'') = ''
OR @SortType < 1 OR @SortType >3
OR @RecorderCount < 0 OR @PageSize < 0 OR @PageIndex < 0
BEGIN
PRINT('ERR_00参数错误')
RETURN
END
IF @SortType = 3
BEGIN
IF (UPPER(RIGHT(@Order,4))!=' ASC' AND UPPER(RIGHT(@Order,5))!=' DESC')
BEGIN
PRINT('ERR_02排序错误') RETURN END
END
DECLARE @new_where1 VARCHAR(8000)
DECLARE @new_where2 VARCHAR(8000)
DECLARE @new_order1 VARCHAR(1000)
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>