AspNetPager paging control stored procedure

Source: Internet
Author: User

Copy codeThe Code is as follows:
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

Alter procedure usp_PagingLarge
@ TableNames VARCHAR (200), -- table name, which can be multiple tables but cannot be aliases
@ PrimaryKey VARCHAR (100), -- primary key, which can be null, but this value cannot be blank when @ Order is null
@ Fields VARCHAR (200), -- the field to be retrieved. It can be a field of multiple tables. It can be null. If it is null, it indicates select *
@ PageSize INT, -- number of records per page
@ CurrentPage INT, -- current page, 0 indicates page 1st
@ Filter VARCHAR (200) = '', -- condition, which can be null. Do not enter where
@ Group VARCHAR (200) = '', -- group basis. It can be empty. You do not need to enter Group
@ Order VARCHAR (200) = ''-- sort. It can be null. If it is null, It is sorted by the primary key in ascending order by default. Order by is not required.
AS
BEGIN
DECLARE @ SortColumn VARCHAR (200)
DECLARE @ Operator CHAR (2)
DECLARE @ SortTable VARCHAR (200)
DECLARE @ SortName VARCHAR (200)
IF @ Fields =''
SET @ Fields = '*'
IF @ Filter =''
SET @ Filter = 'where 1 = 1'
ELSE
SET @ Filter = 'where' + @ Filter
IF @ Group <>''
SET @ Group = 'group by' + @ GROUP

IF @ Order <>''
BEGIN
DECLARE @ pos1 INT, @ pos2 INT
SET @ Order = REPLACE (@ Order, 'asc ', 'asc'), 'desc', 'desc ')
If charindex ('desc', @ Order)> 0
If charindex ('asc ', @ Order)> 0
BEGIN
If charindex ('desc', @ Order) <CHARINDEX ('asc ', @ Order)
SET @ Operator = '<='
ELSE
SET @ Operator = '> ='
END
ELSE
SET @ Operator = '<='
ELSE
SET @ Operator = '> ='
SET @ SortColumn = REPLACE (@ Order, 'asc ', ''), 'desc ',''),'','')
SET @ pos1 = CHARINDEX (',', @ SortColumn)
IF @ pos1> 0
SET @ SortColumn = SUBSTRING (@sortcolumn, 1, @ pos1-1)
SET @ pos2 = CHARINDEX ('.', @ SortColumn)
IF @ pos2> 0
BEGIN
SET @ SortTable = SUBSTRING (@sortcolumn, 1, @ pos2-1)
IF @ pos1> 0
SET @ SortName = SUBSTRING (@ SortColumn, @ pos2 + 1, @ pos1-@ pos2-1)
ELSE
SET @ SortName = SUBSTRING (@ SortColumn, @ pos2 + 1, LEN (@ SortColumn)-@ pos2)
END
ELSE
BEGIN
SET @ SortTable = @ TableNames
SET @ SortName = @ SortColumn
END
END
ELSE
BEGIN
SET @ SortColumn = @ PrimaryKey
SET @ SortTable = @ TableNames
SET @ SortName = @ SortColumn
SET @ Order = @ SortColumn
SET @ Operator = '> ='
END


DECLARE @ type varchar (50)
DECLARE @ prec int
SELECT @ type = t. name, @ prec = c. prec
FROM sysobjects o
JOIN syscolumns c on o. id = c. id
JOIN policypes t on c. xusertype = t. xusertype
WHERE o. name = @ SortTable AND c. name = @ SortName




If charindex ('Char ', @ type)> 0
SET @ type = @ type + '(' + CAST (@ prec AS varchar) + ')'


DECLARE @ TopRows INT
SET @ TopRows = @ PageSize * @ CurrentPage + 1

PRINT @ type
DECLARE @ SQL NVARCHAR (4000)

SET @ SQL = 'Clare @ SortColumnBegin '+ @ type +'
Set rowcount '+ Cast (@ TopRows as VARCHAR (10) + 'select @ SortColumnBegin =' +
@ SortColumn + 'from' + @ TableNames + ''+ @ Filter +'' + @ Group + 'ORDER BY' + @ ORDER +'
Set rowcount '+ CAST (@ PageSize as varchar (10) +'
SELECT '+ @ Fields + 'from' + @ TableNames + ''+ @ Filter + 'and' + @ SortColumn +'' + @ Operator +' @ SortColumnBegin '+ ISNULL (@ group, '') + 'ORDER BY' + @ ORDER +''

-- Print (@ SQL)
Exec (@ SQL)


END

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

Related Article

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.