SQL存储过程中分页

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq389216533/article/details/51953507
</pre><pre name="code" class="sql">ALTER proc [dbo].[GetGoodsLsit]
----分页条件
@StartIndex  varchar(20),
@PageSize    varchar(20),
----排序条件
@SortExpression varchar(50),    --排序字段
@Direction  varchar(10),		--排序方式
@Seltype varchar(100),--查询类别
@Selstr varchar(500)--查询内容
as
begin
 	declare @SQL varchar(6000)
	      
			if @SortExpression <> '' and @Direction <> ''--排序字段
	        BEGIN
			set @SQL='select Row_Number() over (order by  '+@SortExpression+' '+@Direction+') as row, '
			end
		    else
		    BEGIN
	          set @SQL=' select Row_Number() over (order by gl.GoodsId  desc ) as row, '
	        END
  	    set @SQL= @SQL+'   gl.*,PositionsName,PositionsID
					from V_GoodsList gl 
					left join G_Cfg_PositionsList pl on pl.GoodsID=gl.GoodsID where gl.bBlockUp=0'
                --查询条件
                if @Seltype<>''
		begin
		   if @Seltype='GoodsNo'
		   begin
		      set @SQL=@SQL+' and GoodsNo='''+@Selstr+''''
		   end
		   if @Seltype='GoodsName'
		   begin
		      set @SQL=@SQL+' and GoodsName like ''%'+@Selstr+'%'''
		   end
		end

		set @SQL = '  With orderlist as ('+ @SQL +')' 
		if @SortExpression<> '' and @Direction<>''--排序字段
		set @SQL=@SQL+' select *,(select count(*) from orderlist )as count from orderlist where row between (convert(int,'+@StartIndex+')-1)* convert(int,'+@PageSize+')+1 and  (convert(int,'+@StartIndex+') * convert(int,'+@PageSize+') )  order by  '+@SortExpression+' '+@Direction+''
		else
   		set @SQL=@SQL+' select *,(select count(*) from orderlist )as count from orderlist where row between (convert(int,'+@StartIndex+')-1)* convert(int,'+@PageSize+')+1 and  (convert(int,'+@StartIndex+') * convert(int,'+@PageSize+') ); '
        set @SQL=@SQL+' select   * from G_Goods_GoodsSpec where bBlockup=0 ;'
		print @SQL
	    exec(@SQL)

    
end 

猜你喜欢

转载自blog.csdn.net/qq389216533/article/details/51953507