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%>