/ *
Name: spAll_ReturnRows
Input:
Output:
call:
  EXEC spAll_ReturnRows' the SELECT * the FROM table name ', the page number, the number of records returned,' master key ',' sort field '
  spAll_ReturnRows' the FROM all_Categories the SELECT *', 2,10, '[ ID] ',' [ID] '
Description : [one million] General procedure .. storage procedure stored returns the number of returns, records the specified number of pages

* /

CREATE PROCEDURE dbo.spAll_ReturnRows
(
@SQL nVARCHAR(4000),
@Page int,
@RecsPerPage int,
@ID VARCHAR(255),
@Sort VARCHAR(255)
)
AS

DECLARE @Str nVARCHAR(4000)

SET @Str='SELECT   TOP '+CAST(@RecsPerPage AS VARCHAR(20))+' * FROM ('+@SQL+') T WHERE T.'+@ID+' NOT IN
(SELECT   TOP '+CAST((@RecsPerPage*(@Page-1)) AS VARCHAR(20))+' '+@ID+' FROM ('+@SQL+') T9 ORDER BY '+@Sort+') ORDER BY '+@Sort

PRINT @Str

EXEC sp_ExecuteSql @Str
GO

/ *
Name: spAll_DeleteNoneUnique
Input: table names and field list to the query
output:
call:
Description: achieve ten million pages of data display! - can be acquired within 5 seconds 1448 100 million records in the record, page 1200 male does not?
* /

PROCEDURE GetRecordFromPage the CREATE
    @tblName VARCHAR (255), - table
    @fldName varchar (255), - field name
    @PageSize int = 10, - page size
    @PageIndex int = 1, - p
    @IsCount bit = 0, - returns the total number of recorded non-zero value
    @OrderType bit = 0, - set the sort type, a non-zero value in descending
    @strWhere varchar (1000) = '' - query (Note: do not add WHERE)
the AS

declare @strSQL varchar (6000) - Sentences
declare @strTmp varchar (100) - temporary variable
declare @strOrder varchar (400) - type of ordering

if @OrderType != 0
begin
    set @strTmp = "<(select min"
    set @strOrder = " order by [" + @fldName +"] desc"
end
else
begin
    set @strTmp = ">(select max"
    set @strOrder = " order by [" + @fldName +"] asc"
end

set @strSQL = "select top " + str(@PageSize) + " * from ["
    + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
    + @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
    + @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"
    + @strOrder

if @strWhere != ''
    set @strSQL = "select top " + str(@PageSize) + " * from ["
        + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
        + @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
        + @fldName + "] from [" + @tblName + "] where " + @strWhere + " "
        + @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder

if @PageIndex = 1
begin
    set @strTmp = ""
    if @strWhere != ''
        set @strTmp = " where " + @strWhere

    set @strSQL = "select top " + str(@PageSize) + " * from ["
        + @tblName + "]" + @strTmp + " " + @strOrder
end

if @IsCount != 0
    set @strSQL = "select count(*) as Total from [" + @tblName + "]"

exec (@strSQL)

GO