1. 程式人生 > >SQL儲存過程中分頁

SQL儲存過程中分頁

</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  '
[email protected]
+' '[email protected]+') 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 @[email protected]+' and GoodsNo='''[email protected]+''''
		   end
		   if @Seltype='GoodsName'
		   begin
		      set @[email protected]+' and GoodsName like ''%'[email protected]
+'%''' end end

		set @SQL = '  With orderlist as ('+ @SQL +')' 
		if @SortExpression<> '' and @Direction<>''--排序欄位
		set @[email protected]+' select *,(select count(*) from orderlist )as count from orderlist where row between (convert(int,'[email protected]+')-1)* convert(int,'[email protected]+')+1 and  (convert(int,'[email protected]+') * convert(int,'[email protected]+') )  order by  '[email protected]+' '[email protected]+''
		else
   		set @[email protected]+' select *,(select count(*) from orderlist )as count from orderlist where row between (convert(int,'[email protected]+')-1)* convert(int,'[email protected]+')+1 and  (convert(int,'[email protected]tIndex+') * convert(int,'[email protected]+') ); '
        set @[email protected]+' select   * from G_Goods_GoodsSpec where bBlockup=0 ;'
		print @SQL
	    exec(@SQL)

    
end