create proc sp_PublicTurnPageWebSite(
@TBName nvarchar(100)='', --表名,如 pinyin
@PageSize int=10, --每頁的記錄數,默認為 10
@CurPage int=1, --表示當前頁 1
@KeyField nvarchar(100)='ID', --關鍵字段名,默認為 ID,該字段要求是表中的索引 或 無重復和不為空的字段
@KeyAscDesc nvarchar(4)='ASC', --關鍵字的升、降序,默認為升序 ASC , 降序為 DESC
@Fields nvarchar(500)='*', --所選擇的列名,默認為全選
@Condition nvarchar(200)='', --where 條件,默認為空
@Order nvarchar(200)='' --排序條件,默認為空
) with encryption as
if @TBName = ''
begin
raiserror('請指定表名!',11,1)
return
end
if @PageSize <=0 or @CurPage <0
begin
raiserror('當前頁數和每頁的記錄數都必須大於零!',11,1)
return
end
if @KeyAscDesc = 'DESC'
set @KeyAscDesc = '<'
else
set @KeyAscDesc = '>'
if @Condition <> ''
set @Condition = ' where ' + @Condition
declare @SQL nvarchar(2000)
set @SQL = ''
if @CurPage = 1
set @SQL = @SQL + 'Select Top ' + cast(@PageSize as nvarchar(20)) + ' ' + @Fields + ' FROM ' + @TBName + @Condition + ' ' + @Order
else
begin
declare @iTopNum int
set @iTopNum = @PageSize * (@CurPage - 1)
set @SQL = @SQL + 'declare @sLastValue nvarchar(100)' + char(13)
set @SQL = @SQL + 'Select Top ' + cast(@iTopNum as nvarchar(20)) + ' @sLastValue=' + @KeyField + ' FROM ' + @TBName + @Condition + ' ' + @Order + char(13)
declare @Condition2 nvarchar(200)
if @Condition = ''
set @Condition2 = ' where ' + @KeyField + @KeyAscDesc + '@sLastValue '
else
set @Condition2 = ' and ' + @KeyField + @KeyAscDesc + '@sLastValue '
set @SQL = @SQL + 'Select Top ' + cast(@PageSize as nvarchar(20)) + ' ' + @Fields + ' FROM ' + @TBName + @Condition + @Condition2 + @Order
end
EXECUTE sp_executesql @SQL