Efficient jsp+mssql stored procedure common database paging

Source: Internet
Author: User
Tags mssql sort

High efficiency JSP tutorial +mssql stored procedure Common Database tutorial Paging

CREATE PROCEDURE Ture_page

@PageSize int,--Number of rows per page
@PageIndex int,--1 represents the first page
@Col varchar (200),--Fields to display
@Table varchar (200),--the table used, the text of the word to write the content between from and where
@Where varchar (200) = ',--the conditions used
@OKey varchar (50),--Sort fields
@Order varchar = ' ASC '--Sort method

As
DECLARE @cmdstr varchar (2000)
SET NOCOUNT ON

Set @cmdstr = ' Select Top '
Set @cmdstr = @cmdstr +convert (nvarchar, @PageSize)
If @Order = ' DESC ' and @PageIndex > 1
Set @cmdstr = @cmdstr + ' + @Col + ' from ' + @Table + ' where ' + @OKey + ' < '
else if @PageIndex =1
Set @cmdstr = @cmdstr + ' + @Col + ' from ' + @Table + ' where ' + @OKey + ' > = '
Else
Set @cmdstr = @cmdstr + ' + @Col + ' from ' + @Table + ' where ' + @OKey + ' > '
If @PageIndex > 1
Begin
If @Order = ' ASC '
Set @cmdstr = @cmdstr + ' (select max (' + @OKey + ') from (select top)
Else
Set @cmdstr = @cmdstr + ' (select min (' + @OKey + ') from (select top)
Set @cmdstr = @cmdstr +convert (nvarchar, (@PageIndex-1) * @PageSize)
Set @cmdstr = @cmdstr + "+ @OKey + ' from" + @Table + ' ORDER BY ' + @OKey + ' + @Order + ') as T) '
End
Else
Set @cmdstr = @cmdstr + ' 0 '
If @Where <> '
Set @cmdstr = (@cmdstr + ' and ' + @Where + ' ORDER BY ' + @OKey + ' + @Order)
Else
Set @cmdstr = (@cmdstr + ' ORDER BY ' + @OKey + ' + @Order)

Print @cmdstr
EXEC (@cmdstr)
SET NOCOUNT OFF
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.