Three paging Stored Procedure Codes in mmssqlserver

Source: Internet
Author: User
This article introduces the three paging Stored Procedure Implementation codes in mmssqlserver. If you have any need, refer to the example below. Let's take a look.

This article introduces the three paging Stored Procedure Implementation codes in mmssqlserver. If you have any need, refer to the example below. Let's take a look.

-- Based on MAX (MIN) ID

The Code is as follows:
Create proc [dbo]. [proc _ id]
@ Pageindex int = 1, -- current page number
@ Pagesize int = 10, -- size of each page
@ Tablename VARCHAR (50) = '', -- table name
@ Fields VARCHAR (1000) = '', -- query field set
@ Keyid VARCHAR (50) = '', -- primary key
@ Condition NVARCHAR (1000) = '', -- Query condition
@ Orderstr VARCHAR (500), -- sorting Condition
@ TotalRecord bigint output -- total number of records
AS
If isnull (@ orderstr, n') = n' SET @ orderstr = n' ORDER by' + @ keyid + N' des'
If isnull (@ fields, n'') = n' SET @ fields = n '*'
If isnull (@ condition, n') = n' SET @ condition = n' 1 = 1'
DECLARE @ SQL NVARCHAR (4000)
-- IF (@ totalRecord is null)
-- BEGIN
SET @ SQL = n' SELECT @ totalRecord = COUNT (*)'
+ N' from' + @ tablename
+ N 'where' + @ condition
EXEC sp_executesql @ SQL, n' @ totalRecord INT output', @ totalRecord OUTPUT
-- END
IF (@ pageindex = 1)
BEGIN
SET @ SQL = n' SELECT TOP '+ STR (@ pagesize) + n'' + @ fields + n'from' + @ tablename + n'where' + @ condition + n'' + @ orderstr
EXEC (@ SQL)
END
ELSE
BEGIN
DECLARE @ operatestr CHAR (3), @ comparestr CHAR (1)
SET @ operatestr = 'Max'
SET @ comparestr = '>'
IF (@ orderstr <> '')
BEGIN
IF (CHARINDEX ('desc', LOWER (@ orderstr) <> 0)
BEGIN
SET @ operatestr = 'Min'
SET @ comparestr = '<'
END
END
SET @ SQL = n' SELECT top '+ STR (@ pagesize) + N' + @ fields + N' from' + @ tablename + N' where' + @ keyid + @ comparestr
+ N' (SELECT '+ @ operatestr + N' (' + @ keyid + N') FROM '+ @ tablename + N' WHERE' + @ keyid
+ N' IN (select top '+ STR (@ pageindex-1) * @ pagesize) + N' + @ keyid + N' FROM' + @ tablename + N' WHERE'
+ @ Condition + N' + @ orderstr + N') AND '+ @ condition + N' + @ orderstr
EXEC (@ SQL)
END
GO


-- Based on ROW_NUMBER () OVER

The Code is as follows:
Create proc [dbo]. [proc_select_page_row]
@ Pageindex INT = 1, -- current page number
@ Pagesize INT = 10, -- size of each page
@ Tablename VARCHAR (50) = '', -- table name
@ Fields VARCHAR (1000) = '*', -- query field set
@ Keyid VARCHAR (50) = '', -- primary key
@ Condition NVARCHAR (1000) = '', -- Query condition
@ Orderstr VARCHAR (500), -- sorting Condition
@ TotalRecord bigint output -- total number of records
AS
If isnull (@ orderstr, n') = n' SET @ orderstr = n' ORDER by' + @ keyid + N' des'
If isnull (@ fields, n'') = n' SET @ fields = n '*'
If isnull (@ condition, n') = n' SET @ condition = n' 1 = 1'
DECLARE @ SQL NVARCHAR (4000)
-- IF @ totalRecord IS NULL
-- BEGIN
SET @ SQL = n' SELECT @ totalRecord = COUNT (*)'
+ N' from' + @ tablename
+ N 'where' + @ condition
EXEC sp_executesql @ SQL, n' @ totalRecord bigint output', @ totalRecord OUTPUT
-- END
IF (@ pageindex = 1)
BEGIN
SET @ SQL = n' SELECT TOP '+ STR (@ pagesize) + n'' + @ fields + n'from' + @ tablename + n'where' + @ condition + n'' + @ orderstr
EXEC (@ SQL)
END
ELSE
BEGIN
DECLARE @ StartRecord INT
SET @ StartRecord = (@ pageindex-1) * @ pagesize + 1
SET @ SQL = n' SELECT * FROM (SELECT ROW_NUMBER () OVER ('+ @ orderstr + N') AS rowId, '+ @ fields + N' from' + @ tablename + N') as t where rowId> =' + STR (@ StartRecord) + N' and rowId <= '+ STR (@ StartRecord + @ pagesize-1)
EXEC (@ SQL)
END
GO


-- Based on the TOP ID

The Code is as follows:
Create proc [dbo]. [proc_select_page_top]
@ Pageindex INT = 1, -- current page number
@ Pagesize INT = 10, -- size of each page
@ Tablename VARCHAR (50) = '', -- table name
@ Fields VARCHAR (1000) = '', -- query field set
@ Keyid VARCHAR (50) = '', -- primary key
@ Condition NVARCHAR (1000) = '', -- Query condition
@ Orderstr VARCHAR (500), -- sorting Condition
@ TotalRecord bigint output -- total number of records
AS
If isnull (@ orderstr, n') = n' SET @ orderstr = n' ORDER by' + @ keyid + N' des'
If isnull (@ fields, n'') = n' SET @ fields = n '*'
If isnull (@ condition, n') = n' SET @ condition = n' 1 = 1'
DECLARE @ SQL NVARCHAR (4000)
-- IF (@ totalRecord is null)
-- BEGIN
SET @ SQL = n' SELECT @ totalRecord = COUNT (*)'
+ N' from' + @ tablename
+ N 'where' + @ condition
EXEC sp_executesql @ SQL, n' @ totalRecord INT output', @ totalRecord OUTPUT
-- END
IF (@ pageindex = 1)
BEGIN
SET @ SQL = n' SELECT TOP '+ STR (@ pagesize) + n'' + @ fields + n'from' + @ tablename + n'where' + @ condition + n'' + @ orderstr
EXEC (@ SQL)
END
ELSE
BEGIN
SET @ SQL = n' SELECT TOP '+ STR (@ pagesize) + N' + @ fields + N' FROM' + @ tablename + N' WHERE '+ @ keyid
+ N' not in (select top '+ STR (@ pageindex-1) * @ pagesize) + N' + @ keyid + N' FROM'
+ @ Tablename + N 'where' + @ condition + N' + @ orderstr + N') AND '+ @ condition + N' + @ orderstr
EXEC (@ SQL)
END
GO

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.