페이지별 저장 프로세스 요약

12301 단어 저장 프로세스
/*********************************************************************/

/*       MSSQL  :        TOP MAX/TOP MIN            */

/*                        2010/07/09                                 */

/*                         Leo.wl                                    */

/*********************************************************************/

CREATE PROCEDURE [dbo].[getPagerTopMAX] (

    @tblName     nvarchar(2000),           ----            , :"myTable"

    @ID        nvarchar(150),            ----     , :"id"

    @page        int = 1,                ----         

    @pageSize    int = 10,                ----         

    @fldName     nvarchar(500) = '*',        ----        , :"id, cdsinger, cdsingertype, area, cdsingerreadme"

    @fldSort    nvarchar(200) = @ID,       ----         

    @Sort        bit = 1,                ----    ,0   ,1   (        Sort               (              )--     :' SortA Asc,SortB Desc,SortC ')

    @strCondition    nvarchar(1000) = null, ----    ,  where, :"and a=1 and b=2"

    @Dist                 bit = 0,          ----          DISTINCT   0   /1  

    @Counts    int = 1 output,              ----       

    @pageCount    int = 1 output            ----           

) AS begin

    SET NOCOUNT ON

    Declare @sqlTmp nvarchar(3000)           ----       SQL  

    Declare @strTmp nvarchar(3000)            ----               

    Declare @strID     nvarchar(3000)       ----           ID     



    Declare @strSortType nvarchar(10)        ----      A

    Declare @strFSortType nvarchar(10)        ----      B



    Declare @SqlSelect nvarchar(50)         ----   DISTINCT     SQL  

    Declare @SqlCounts nvarchar(50)         ----   DISTINCT       SQL  





    if @Dist  = 0

    begin

        set @SqlSelect = 'select '

        set @SqlCounts = 'Count(*)'

    end

    else

    begin

        set @SqlSelect = 'select distinct '

        set @SqlCounts = 'Count(DISTINCT '+@ID+')'

    end





    if @Sort=0

    begin

        set @strFSortType=' ASC '

        set @strSortType=' DESC '

    end

    else

    begin

        set @strFSortType=' DESC '

        set @strSortType=' ASC '

    end







    --------      --------



    --  @strTmp            

    if @strCondition is null or @strCondition=''     --        

    begin

        set @sqlTmp =  @fldName + ' From ' + @tblName

        set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName

        set @strID = ' From ' + @tblName

    end

    else

    begin

        set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition

        set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition

        set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition

    end



    ----         -----

    exec sp_executesql @strTmp,N'@Counts int out ',@Counts out

    declare @tmpCounts int

    if @Counts = 0

        set @tmpCounts = 1

    else

        set @tmpCounts = @Counts



        --      

        set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize



        /**//**              **/

        if @page>@pageCount

            set @page=@pageCount



        --/*-----    2   -------*/

        declare @pageIndex int --  /   

        declare @lastcount int --  %    



        set @pageIndex = @tmpCounts/@pageSize

        set @lastcount = @tmpCounts%@pageSize

        if @lastcount > 0

            set @pageIndex = @pageIndex + 1

        else

            set @lastcount = @pagesize



        --//***    

        if @strCondition is null or @strCondition=''     --        

        begin

            if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --        

                begin 

                    if @page=1

                        set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(20))+' '+ @fldName+' from '+@tblName                        

                            +' order by '+ @fldSort +' '+ @strFSortType

                    else

                    begin                    

                        set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(20))+' '+ @fldName+' from '+@tblName

                            +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName

                            +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'

                            +' order by '+ @fldSort +' '+ @strFSortType

                    end    

                end

            else

                begin

                set @page = @pageIndex-@page+1 --        

                    if @page <= 1 --                        

                        set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(20))+' '+ @fldName+' from '+@tblName

                            +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 

                    else

                        set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(20))+' '+ @fldName+' from '+@tblName

                            +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName

                            +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'

                            +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 

                end

        end



        else --     

        begin

            if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --        

            begin

                    if @page=1

                        set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(20))+' '+ @fldName+' from '+@tblName                        

                            +' where 1=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType

                    else

                    begin                    

                        set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(20))+' '+ @fldName+' from '+@tblName

                            +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName

                            +' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'

                            +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType

                    end            

            end

            else

            begin 

                set @page = @pageIndex-@page+1 --        

                if @page <= 1 --        

                        set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(20))+' '+ @fldName+' from '+@tblName

                            +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType                     

                else

                        set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(20))+' '+ @fldName+' from '+@tblName

                            +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName

                            +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'

                            +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType                

            end    

        end



    ------      -----

    --print @strTmp

    exec (@strTmp)





    SET NOCOUNT OFF



end

/*********************************************************************/

/*  MSSQL  :          ROW_NUMBER() OVER           */

/*                        2010/07/09                                 */

/*                         Leo.wl                                    */

/*********************************************************************/

CREATE PROCEDURE [dbo].[getPagerROWOVER] 

    @PageIndex int = 1,

    @PageSize int = 10,

    @Tables nvarchar(1000) = NULL,

    @Fields nvarchar(2000) = '*',

    @Where nvarchar(2000) = NULL,

    @GroupBy nvarchar(2000) = NULL,

    @OrderBy nvarchar(1000) = NULL,

    @ReturnCount int = 0 OUTPUT

AS

BEGIN

    SET NOCOUNT ON;

    declare @Sql nvarchar(4000),@startRowIndex int, @maximumRows int;

    declare @strWhere nvarchar(2000)

    declare @strGroupBy nvarchar(2000)



    set @startRowIndex = (@PageIndex - 1) * @PageSize;

    set @maximumRows = @PageSize;



    IF @Where IS NOT NULL AND @Where != ''

    BEGIN

        SET @strWhere = ' WHERE ' + @Where

    END

    ELSE

    BEGIN

        SET @strWhere = ''

    END

    IF @GroupBy IS NOT NULL AND @GroupBy != ''

    BEGIN

        SET @strGroupBy = ' GROUP BY ' + @GroupBy

    END

    ELSE

    BEGIN

        SET @strGroupBy = ''

    END

        

    set @Sql = 'select ' + @Fields + ' from (select ' + @Fields + ', ROW_NUMBER() OVER (ORDER BY ' + @OrderBy + ') as rownum from ' + @Tables + @strWhere + @strGroupBy + ') as tmpTable where rownum > ' + CONVERT(nvarchar(10), @startRowIndex) + ' and rownum <= (' + CONVERT(nvarchar(10), @startRowIndex) + ' + ' + CONVERT(nvarchar(10), @maximumRows) + ')'

    exec(@Sql)

    print @sql

    set @Sql = 'select @ReturnCount=COUNT(*) from ' + @Tables + @strWhere

    exec sp_executesql @Sql,N'@ReturnCount int OUTPUT',@ReturnCount OUTPUT

    print @sql

END



/*********************************************************************/

/*                    MSSQL  :                            */

/*                        2010/07/09                                 */

/*                         Leo.wl                                    */

/*********************************************************************/

CREATE     PROCEDURE [dbo].[Pagination_dongde_2]

(

@tab nvarchar(1000),---  

@strFld nvarchar(2500), --     

@strWhere varchar(8000), --where   

@Page int, --  

@RecsPerPage int, --        

@ID VARCHAR(255), --         ID 

@Sort VARCHAR(255), --       

@isGetCount bit--        

)

AS



DECLARE @Str nVARCHAR(4000)



declare @SQL nVARCHAR(4000) --       SQL  

set @SQL='select ' + @strFld + ' from '+ @tab +' where ' +@strWhere



if(@isGetCount=0)

begin



SET @Str='SELECT TOP '+CAST(@RecsPerPage AS VARCHAR(20))+' * FROM 

('+@SQL+') bb  WHERE bb.'+@ID+' NOT IN (SELECT TOP '+CAST((@RecsPerPage*(@Page-1)) 

AS VARCHAR(20))+' '+@ID+' FROM ('+@SQL+') aa  ORDER BY '+@Sort+') ORDER BY '+@Sort



PRINT @Str



end



else

begin



SET @Str='select count('+@ID+') from '+ @tab + ' where ' +@strWhere



PRINT @Str

end



EXEC sp_ExecuteSql @Str


 

좋은 웹페이지 즐겨찾기