For more common paging of stored procedures, you need to give more advice!

Source: Internet
Author: User

 
-- Name: Create a paging Stored Procedure
-- Time: 2011-07-04
-- Author: Wuhan
Alter proc proc_wuhan_getpages -- create a stored procedure
@ Pagesize int, -- page size
@ Currentpage int, -- current page (page number)
@ Tablename nvarchar (100), -- table name
@ Columns nvarchar (1000), -- field name
@ Condition nvarchar (100), -- Query Condition
@ Ordertype int, -- sort type (0 is ascending, 1 is descending)
@ Ordercolumn nvarchar (100), -- Sort Field
@ Premarykeycolumn nvarchar (100) -- primary key field name
As
Begin
Declare @ strsqlstatement nvarchar (2000) -- the last statement executed by the Stored Procedure
Declare @ chartype nvarchar (10)
Begin
If @ condition = ''-- the query condition is null.
Begin
If @ ordertype = 0 -- Ascending
Begin
Set @ chartype = 'asc'
Set @ strsqlstatement = 'select top' + ltrim (@ pagesize) +'
'+ @ Columns + 'from' + @ tablename + 'where ('
+ @ Premarykeycolumn + 'not in (select top '+ convert (char, @ pagesize * (@ currentPage-1) + @ premarykeycolumn
+ 'From' + @ tablename + 'ORDER BY' + @ ordercolumn + @ chartype + ') Order'
+ @ Ordercolumn + @ chartype
End
Else if @ ordertype = 1
Begin
Set @ chartype = 'desc' -- descending order
Set @ strsqlstatement = 'select top' + ltrim (@ pagesize) +'
'+ @ Columns + 'from' + @ tablename + 'where ('
+ @ Premarykeycolumn + 'not in (select top '+ convert (char, @ pagesize * (@ currentPage-1) + @ premarykeycolumn
+ 'From' + @ tablename + 'ORDER BY' + @ ordercolumn + @ chartype + ') Order'
+ @ Ordercolumn + @ chartype
End
Print @ strsqlstatement
Exec (@ strsqlstatement) -- output the last statement to be executed
End
Else if @ condition! = ''-- The query condition is not empty.
Begin
If @ ordertype = 0 -- Ascending
Begin
Set @ chartype = 'asc'
Set @ strsqlstatement = 'select top' + ltrim (@ pagesize) +'
'+ @ Columns + 'from' + @ tablename + 'where ('
+ @ Premarykeycolumn + 'not in (select top '+ convert (char, @ pagesize * (@ currentPage-1) + @ premarykeycolumn
+ 'From' + @ tablename + 'ORDER BY' + @ ordercolumn + @ chartype + ') and' + @ condition + ') Order'
+ @ Ordercolumn + @ chartype
End
Else if @ ordertype = 1
Begin
Set @ chartype = 'desc' -- descending order
Set @ strsqlstatement = 'select top' + ltrim (@ pagesize) +'
'+ @ Columns + 'from' + @ tablename + 'where ('
+ @ Premarykeycolumn + 'not in (select top '+ convert (char, @ pagesize * (@ currentPage-1) + @ premarykeycolumn
+ 'From' + @ tablename + 'ORDER BY' + @ ordercolumn + @ chartype + ') and' + @ condition + ') Order'
+ @ Ordercolumn + @ chartype
End
Print @ strsqlstatement
Exec (@ strsqlstatement) -- output the last statement to be executed
End
Else
Print 'An error occurred. I still need to practice more! '+ Ltrim (@ pagesize)
End
End

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.