Paging SQL Server Stored Procedure

Source: Internet
Author: User
Tags rowcount

/* -- Paging program implemented by stored procedures
Displays page X of the specified table, view, and query result.
In the case of primary keys or ID columns in the table, query the data directly from the original table. In other cases, use the temporary table method.
This method is not recommended if the view or query result contains a primary key.
-- Producer build 2003.09 --*/
/* -- Call example
Exec p_show 'region information'
Exec p_show 'region information', 5, 3, 'region No., Region name, mnemonic code ', 'region No'
--*/
/*
The query statements with sorting must be sorted first and then output the result. That is:
Exec p_show 'select top 100 percent * from region data order by region name', 'region number, Region name, mnemonic code ', 'region name'
-- Query statement plus: top 100 percent // top
*/
If exists (select * from dbo. sysobjects where id = object_id (n' [dbo]. [p_show] ') and OBJECTPROPERTY (id, n' IsProcedure') = 1)
Drop procedure [dbo]. [p_show]
GO
Create Proc p_show
@ QueryStr nvarchar (4000), -- table name, view name, query statement
@ PageSize int = 10, -- size of each page (number of rows)
@ PageCurrent int = 1, -- the page to be displayed
@ FdShow nvarchar (4000) = '', -- List of fields to be displayed. If the query result contains an ID field, you need to specify this value without the ID field.
@ FdOrder nvarchar (1000) = ''-- List of sorting Fields
As
Declare @ FdName nvarchar (250) -- primary key or column name in the table or temporary table
, @ Id1 varchar (20), @ Id2 varchar (20) -- start and end record numbers
, @ Obj_ID int -- Object ID
-- Processing of compound primary keys in the table
Declare @ strfd nvarchar (2000) -- composite Primary Key List
, @ Strjoin nvarchar (4000) -- connection Field
, @ Strwhere nvarchar (2000) -- Query Condition

Select @ Obj_ID = object_id (@ QueryStr)
, @ FdShow = case isnull (@ FdShow, '') when ''then'' * 'else' + @ FdShow end
, @ FdOrder = case isnull (@ FdOrder, '') when ''then'' else 'ORDER BY' + @ FdOrder end
, @ QueryStr = case when @ Obj_ID is not null then ''+ @ QueryStr else '(' + @ QueryStr + ') a' end
-- If the first page is displayed, top can be used directly.
If @ PageCurrent = 1
Begin
Select @ Id1 = cast (@ PageSize as varchar (20 ))
Exec ('select top '+ @ Id1 + @ FdShow + 'from' + @ QueryStr + @ FdOrder)
Return
End
-- If it is a table, check whether there is an identifier or a primary key in the table.
If @ Obj_ID is not null and objectproperty (@ Obj_ID, 'istable') = 1
Begin
Select @ Id1 = cast (@ PageSize as varchar (20 ))
, @ Id2 = cast (@ PageCurrent-1) * @ PageSize as varchar (20 ))
Select @ FdName = name from syscolumns where id = @ Obj_ID and status = 0x80
If @ rowcount = 0 -- if the table has no ID column, check whether the table has a primary key.
Begin
If not exists (select 1 from sysobjects where parent_obj = @ Obj_ID and xtype = 'pk ')
Goto lbusetemp -- if the table has no primary key, use a temporary table for processing.
Select @ FdName = name from syscolumns where id = @ Obj_ID and colid in (
Select colid from sysindexkeys where @ Obj_ID = id and indid in (
Select indid from sysindexes where @ Obj_ID = id and name in (
Select name from sysobjects where xtype = 'pk' and parent_obj = @ Obj_ID
)))
If @ rowcount> 1 -- check whether the primary key in the table is a composite primary key
Begin
Select @ strfd = '', @ strjoin ='', @ strwhere =''
Select @ strfd = @ strfd + ', [' + name + ']'
, @ Strjoin = @ strjoin + 'and a. [' + name + '] = B. [' + name + ']'
, @ Strwhere = @ strwhere + 'and B. [' + name + '] is null'
From syscolumns where id = @ Obj_ID and colid in (
Select colid from sysindexkeys where @ Obj_ID = id and indid in (
Select indid from sysindexes where @ Obj_ID = id and name in (
Select name from sysobjects where xtype = 'pk' and parent_obj = @ Obj_ID
)))
Select @ strfd = substring (@ strfd, 2,2000)
, @ Strjoin = substring (@ strjoin, 5)
, @ Strwhere = substring (@ strwhere, 5)
Goto lbusepk
End
End
End
Else
Goto lbusetemp
/* -- Use the identification column or primary key as a single field --*/
Lbuseidentity:
Exec ('select top '+ @ Id1 + @ FdShow + 'from' + @ QueryStr
+ 'Where' + @ FdName + 'not in (select top'
+ @ Id2 + ''+ @ FdName + 'from' + @ QueryStr + @ FdOrder
+ ')' + @ FdOrder
)
Return
/* -- The table has a composite primary key --*/
Lbusepk:
Exec ('select' + @ FdShow + 'from (select top' + @ Id1 + 'a. * from
(Select top 100 percent * from '+ @ QueryStr + @ FdOrder +')
Left join (select top '+ @ Id2 + ''+ @ strfd +'
From '+ @ QueryStr + @ FdOrder +') B on '+ @ strjoin +'
Where '+ @ strwhere +')'
)
Return
/* -- Use a temporary table --*/
Lbusetemp:
Select @ FdName = '[ID _' + cast (newid () as varchar (40) + ']'
, @ Id1 = cast (@ PageSize * (@ PageCurrent-1) as varchar (20 ))
, @ Id2 = cast (@ PageSize * @ PageCurrent-1 as varchar (20 ))
Exec ('select' + @ FdName + '= identity (int, 0, 1),' + @ FdShow +'
Into # tb from '+ @ QueryStr + @ FdOrder +'
Select '+ @ FdShow +' from # tb where '+ @ FdName + 'beten'
+ @ Id1 + 'and' + @ Id2
)
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.