createPROCEDURE tp_Fetch_List( @page_numINT, @row_in_pageINT, @order_columnVARCHAR(50), @row_totalINT OUTPUT, @comb_conditionVARCHAR(500) ) AS BEGIN SET NOCOUNT ON DECLARE @jcc_statusINT, @sqlNVARCHAR(4000), @row_aheadINT SET@jcc_status=0 SET@row_ahead= (@page_num-1) *@row_in_page SET@sql='SELECT TOP '+cast(@row_in_pageasvarchar(255)) +' * FROM ( ' SET@sql=@sql+'SELECT * FROM TestGrid ) as A where 1=1' IFLEN(@comb_condition)>0 SET@sql=@sql+' AND ('+@comb_condition+')' SET@sql=@sql+'and UserID not in ( select UserID from (' SET@sql=@sql+'SELECT TOP '+cast(@row_aheadasvarchar(255)) +' * From (' SET@sql=@sql+'SELECT * FROM TestGrid ) as A where 1=1' IFLEN(@comb_condition)>0 SET@sql=@sql+' AND ( '+@comb_condition+' )' IFLEN(@order_column)>0 BEGIN SET@sql=@sql+' ORDER BY '+@order_column+' ) AS B )' END ELSE BEGIN SET@sql=@sql+' ) AS B )' END IFLEN(@order_column)>0 BEGIN SET@sql=@sql+' ORDER BY '+@order_column END print@sql EXEC (@sql) SET@sql= N'SELECT @row_total=COUNT(*) FROM (' SET@sql=@sql+'SELECT * FROM TestGrid ) as A where 1=1' IFLEN(@comb_condition)>0 SET@sql=@sql+' AND ('+@comb_condition+')' print@sql EXEC sp_executesql @sql,N'@row_total INT OUT',@row_total OUT IF@@ERROR!=0 BEGIN SELECT@jcc_status=-98 END exit_bk: -- exit with MS SQL Server error IF@jcc_status=-98 BEGIN RAISERROR ('MS SQL Server error, please contact your system administrator.',16,1)WITH NOWAIT RETURN (@jcc_status) END -- normal exit RETURN (0) END GO -- declare @aa int -- exec tp_Fetch_List 1,10,'',@aa out ,'1=1' -- select @aa