Efficient MSSQL Stored procedure paging code (1/6)

Source: Internet
Author: User
Tags mssql rtrim table name

In general small data paging is that we use a simple paging function on it, but if the millions data paging, then I have to consider the efficient MSSQL stored procedure paging code OH.
DECLARE @TotalCount int
DECLARE @TotalPageCount int
exec p_viewpage_a ' type1 ', ' * ', ' id ', ', ' ID ASC ', 1,0,4,3, @TotalCount output, @TotalPageCount output

SELECT * FROM Type1


Create PROC p_viewpage_a
/*
Nzperfect [No_miss] Efficient common paging stored procedure (bidirectional retrieval) 2007.5.7 qq:34813284
Warning: Tables or views that apply to a single primary key or existence of a unique value column
PS Tutorial: SQL statement is 8000 bytes, please note that the incoming parameter and the total SQL length do not exceed the specified range
*/
@TableName VARCHAR (200),--table name
@FieldList VARCHAR (2000),--Display column names, if all fields are *
@PrimaryKey VARCHAR (100),--single primary key or unique value key
@Where VARCHAR (2000),--the query condition does not contain a ' Where ' character, such as Id>10 and Len (userid) >9
@Order VARCHAR (1000),--sorting does not contain an ' order by ' character, such as ID Asc,userid desc, you must specify ASC or DESC
-Note that when the @sorttype=3 comes into effect, remember to add the primary key at the end, otherwise it will make you more depressed
@SortType INT,--collation 1: Positive order ASC 2: Reverse DESC 3: Multi-column Sorting method
@RecorderCount INT,--Total records 0: Returns the total record
@PageSize INT--number of records output per page
@PageIndex INT,--current page 111cn.net
@TotalCount INT OUTPUT,--Returns the total record
@TotalPageCount INT OUTPUT--Total number of pages returned
As
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

Home 1 2 3 4 5 6 last

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.