SQL Server 2000 efficient paging storage process

Source: Internet
Author: User
Tags rtrim
Code


Alter PROCEDURE Pages
@ 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 (800), -- 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
@ Order VARCHAR (200) = '', -- sort. It can be null. If it is null, It is sorted by the master key in ascending order by default. Order by is not required.
@ ResultCount varchar (24)
AS
BEGIN
Declare @ topRow varchar (12)
Declare @ tempPageSize varchar (12)
If (len (@ Order)> 0)
Begin
Set @ Order = 'ORDER BY' + @ order
End
Else
Begin
Set @ Order =''
End
If (len (@ Filter) <1)
Begin
Set @ Filter = '1 = 1'
End
If (@ CurrentPage-1 <= 0)
Set @ CurrentPage = 0
If (len (rtrim (ltrim (@ ResultCount)> 0)
Set @ ResultCount = 'set rowcount' + ltrim (rtrim (@ ResultCount ))
Set @ topRow = rtrim (ltrim (str (@ PageSize * (@ CurrentPage-1 ))))
Set @ tempPageSize = rtrim (ltrim (str (@ PageSize )))
Exec ('
Declare @ temptable table (rownum int identity (1, 1), Gid varchar (36) '+'
Declare @ datatable table (Gid varchar (36) '+'
Declare @ date datetime '+'
Set @ date = getdate () '+'
Set nocount on '+'
'+ @ ResultCount +'
Insert into @ temptable (Gid) select '+ @ PrimaryKey + 'from' + @ TableNames + 'where' + @ Filter + @ Order +'
Set rowcount '+ @ tempPageSize +'
Insert into @ datatable (Gid) select Gid from @ temptable where rownum> '+ @ topRow +'
Select '+ @ Fields + 'from' + @ TableNames + 'where' + @ Filter + 'and' + @ PrimaryKey +' in (select Gid from @ datatable) '+ @ Order +'
Set rowcount 0' +'
Print (datediff (MS, @ date, getdate ()))')
-- Declare @ datatable table (Gid varchar (12) '+'
--- Insert into @ datatable (Gid) select Gid from @ temptable where rownum> '+ @ topRow +'
-- Set rowcount '+ @ tempPageSize +'
-- + 'Set rowcount 0'
-- Print ('insert into @ temptable (Gid) select' + @ PrimaryKey + 'from' + @ TableNames + 'where' + @ Filter + @ Order)
--- Print ('select' + @ Fields + 'from' + @ TableNames + 'where' + @ Filter + 'and' + @ PrimaryKey + 'in (select Gid from @ datatable) '+ @ Order)
End
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.