declare @sLastValue nvarchar(100)
SELECT Top 25 @sLastValue=Click FROM table1 order by Click Desc
SELECT Top 25 id FROM table1 where Click<@sLastValue order by Click Desc 按click排序的,如果click的列里面有许多值为0,则这些值为0的数据则全部都显示不出来,大于0就可以,怎么办?
SELECT Top 25 @sLastValue=Click FROM table1 order by Click Desc
SELECT Top 25 id FROM table1 where Click<@sLastValue order by Click Desc 按click排序的,如果click的列里面有许多值为0,则这些值为0的数据则全部都显示不出来,大于0就可以,怎么办?
SELECT Top 25 @sLastValue=Click FROM table1 order by Click Desc--当@sLastValue=0时,根据Click<0 这个条件,自然为0的记录都不显示
SELECT Top 25 id FROM table1 where Click<@sLastValue order by Click Desc
@TBName nvarchar(2000)='',--表名,如 pinyin
@PageSize int=10,--每页的记录数,默认为 10
@PageIndex int=1,--表示当前页 1
@KeyField nvarchar(100)='ID',--关键字段名,默认为 ID,该字段要求是表中的索引 或 无重复和不为空的字段
@KeyAscDesc nvarchar(4)='ASC',--关键字的升、降序,默认为升序 ASC , 降序为 DESC
@Fields nvarchar(2000)='*',--所选择的列名,默认为全选
@Condition nvarchar(2000)='',--where 条件,默认为空
@Order nvarchar(200)=''--排序条件,默认为空
as
if @TBName = ''
begin
raiserror('请指定表名!',11,1)
return
end
if @PageSize <=0 or @PageIndex < 0
begin
raiserror('当前页数和每页的记录数都必须大于零!',11,1)
return
end
if @KeyAscDesc = 'DESC'
set @KeyAscDesc = '<'
else
set @KeyAscDesc = '>'
if @Condition <> ''
set @Condition = ' where ' + @Conditiondeclare @SQL nvarchar(4000)set @SQL = ''
if @PageIndex = 1
set @SQL = @SQL + 'SELECT Top ' + cast(@PageSize as nvarchar(20)) + ' ' + @Fields + ' FROM ' + @TBName + @Condition + ' ' + @Order
else
begin
declare @iTopNum int
set @iTopNum = @PageSize * (@PageIndex - 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(2000)
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
endEXECUTE sp_executesql @SQL