Stored Procedures | paging
Paging stored procedures, their own "not_in" and "SET rowcount" a combination of two ways.
The "SET rowcount" algorithm does not change, and the "not_in" sort adds a primary key field so that the speed increases a lot and the result is one-way only, but not reversible. "SET rowcount" does not have a speed problem, "not_in" sort after the primary key field in the 30W record on the non-indexed field, the sort of non-aggregate field to flip a page but 3 seconds, also OK: P
----------------------
CREATE PROCEDURE Paging_custom
(
@Tables varchar (1000),
@PK varchar (100),
@Sort varchar = NULL,
@isAscending Bit=1,
@PageNumber int = 1,
@PageSize int = 10,
@Fields varchar (1000) = ' * ',
@Filter varchar (1000) = NULL,
@Group varchar (1000) = NULL)
As
/*default sorting*/
IF @Sort is NULL OR @Sort = '
SET @Sort = @PK
/*set Filter & Group variables.*/
IF @Filter is not NULL and @Filter!= '
BEGIN
SET @strFilter = ' WHERE ' + @Filter + '
SET @strSimpleFilter = ' and ' + @Filter + '
End
ELSE
BEGIN
SET @strSimpleFilter = '
SET @strFilter = '
End
IF @Group is not NULL and @Group!= '
SET @strGroup = ' GROUP by ' + @Group + '
ELSE
Begin
SET @strGroup = '
End
/*set sorting variables.*/
SET @strSortColumn = @Sort
/*operator and asc_desc*/
DECLARE @strAsc_Des varchar (10)
IF @isAscending = 0
BEGIN
SET @operator = ' <= '
SET @strAsc_Des = ' Desc '
End
ELSE
BEGIN
SET @operator = ' >= '
SET @strAsc_Des = ' ASC '
End
IF CHARINDEX ('. ', @PK) > 0
BEGIN
SET @strPKColumn = SUBSTRING (@PK, 0, CHARINDEX ('. ', @PK))
End
ELSE
BEGIN
SET @strPKColumn = @PK
End
IF CHARINDEX ('. ', @strSortColumn) > 0
BEGIN
SET @SortTable = SUBSTRING (@strSortColumn, 0, CHARINDEX ('. ', @strSortColumn))
SET @SortName = SUBSTRING (@strSortColumn, CHARINDEX ('. ', @strSortColumn) + 1, LEN (@strSortColumn))
End
ELSE
BEGIN
SET @SortTable = @Tables
SET @SortName = @strSortColumn
End
/*handler Complex table*/
--be Join Table,so get the left table
IF CHARINDEX (' join ', @Tables) > 0
BEGIN
SET @SortTable =substring (LTrim (@Tables), 0,charindex (", @Tables)"
SET @strSortColumn = @SortTable + '. ' + @SortName
End
/*check the SortColumn if be unique*/
DECLARE @tempName varchar (100)
IF @SortName <> @strPKColumn
Begin
SELECT @tempName =b.name
From sysobjects a INNER JOIN
sysobjects b on a.id = B.parent_obj
INNER join sysindexes c on b.name = C.name INNER Join
Sysindexkeys D on c.id = d.id and C.indid = D.indid INNER JOIN
syscolumns e on d.id = e.id and d.colid = E.colid
WHERE (B.xtype = ' UQ ') and (A.name = @SortTable) and (e.name = @SortName)
IF @tempName is NULL goto:paging_not_in
End
DECLARE @type varchar (100)
DECLARE @prec int
SELECT @type =t.name, @prec =c.prec
From sysobjects o
JOIN syscolumns C on o.id=c.id
JOIN systypes T on C.xusertype=t.xusertype
WHERE o.name = @SortTable and c.name = @SortName
/*the left table doesn ' t contain sortcolumn * *
--if @type is null or @type = ' goto:paging_not_in
IF CHARINDEX (' char ', @type) > 0
SET @type = @type + ' (' + CAST (@prec as varchar) + ') '
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.