페이지별 저장 프로세스 (1): 페이지별로 한 테이블의 데이터를 얻습니다. (검증되지 않았습니다.)

22220 단어 저장 프로세스
-------------------------------------------------------------------------

--   (Name):GetRecordFromSingleTableByPage

--   (Function):          

--    (Author):

--   (Date):2006 02 15 

--/*

    @tblName      nvarchar(255),        --   

    @priKeyName      nvarchar(50),            --    

    @fldNames     nvarchar(1000),       --        ,          

    @PageSize     int = 0,              --    ,0       

    @PageIndex    int = 1,              --   , 1  

    @OrderType    nvarchar(200) = '',   --     ,'':       0:     1:     else:         

    @strWhere     nvarchar(2000) = '',  --      (  :     where)

*/

--    :

--    @TotalItem int output,    --      

--    @TotalPage int output    --         

--   :                 

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

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

ALTER    PROCEDURE [dbo].[GetRecordFromSingleTableByPage]

    @tblName      nvarchar(255),        --   

    @priKeyName      nvarchar(50),        --        

    @fldNames     nvarchar(1000),       --    ,          

    @PageSize     int = 0,              --    

    @PageIndex    int = 1 output,              --   

    @OrderType    nvarchar(200) = '',   --     ,'':       01:        :         

    @strWhere     nvarchar(2000) = '',  --      (  :     where)

    @TotalItem int output,    

    @TotalPage int output

AS



declare @strByPage   nvarchar(4000)   --       

declare @strNoPage nvarchar(1500)      --        ,           

declare @strTotal nvarchar(1000)      --           

declare @strNonResult nvarchar(1500)  --         

declare @strTmp nvarchar(100)

declare @strOrder nvarchar(200)



if @OrderType is null or @OrderType=''

begin

    set @strOrder = ''

    set @strTmp=''

end

else if @OrderType = '0' --   

begin

    set @strTmp = @priKeyName + '>(select max([' + @priKeyName + ']) from '

    set @strOrder = ' order by ' + @priKeyName + ' asc'

end

else if @OrderType = '1' --   

begin

    set @strTmp = @priKeyName + '<(select min([' + @priKeyName + ']) from '

    set @strOrder = ' order by ' + @priKeyName + ' desc'

end

else --          

begin

    set @strTmp = ''

    set @strOrder = ' order by ' + @OrderType

end





set @strNonResult = 'select ' + @fldNames + ' from ' + @tblName + ' where 1=2'





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

begin

    set @strTotal = N'select @TotalItem = count(*) from ' + @tblname 

    set @strNoPage = N'select ' + @fldNames + ' from ' + @tblName + @strOrder

end

else

begin

    set @strTotal = N'select @TotalItem = count(*) from ' + @tblname + ' where ' + @strWhere

    set @strNoPage = N'select ' + @fldNames + ' from ' + @tblName + ' where ' + @strWhere + @strOrder

end



--               

exec sp_executeSql @strTotal,N'@TotalItem int output',@TotalItem output

--             ,              

if @TotalItem = 0

begin

    set @TotalPage = 0

    exec sp_executeSql @strNonResult    

    return 0

end

--if @PageSize = 0   --        

begin

    set nocount off

    set @TotalPage = 0

    exec sp_executeSql @strNoPage

    return

end

else    --       

begin

    set nocount on

    --        ,     ,   1  

    set @TotalPage=CEILING(cast(@TotalItem as float)/@PageSize)

    if(@PageIndex>@TotalPage)

        set @PageIndex=@TotalPage

    if(@PageIndex <1)

        set @PageIndex=1

    if @PageIndex =1    --       

    begin

        if @strWhere=''

            set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldNames + ' from ' + @tblName + @strOrder

        else

            set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldNames + ' from ' + @tblName + ' where ' + @strWhere + @strOrder

    end

    else    --    

    begin

        

        if (@OrderType='0' or @OrderType='1') --         

        begin

            if @strWhere=''

                set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldnames 

                    + ' from ' + @tblName 

                    + ' where ' + @strTmp

                    + ' (select top ' + cast((@PageIndex-1) * @PageSize as varchar) + ' ' + @priKeyName 

                    + ' from ' + @tblName + @strOrder + ') as tmptbl)'

                    + @strOrder

            else

                set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldnames 

                    + ' from ' + @tblName 

                    + ' where ' + @strTmp

                    + ' (select top '+ cast((@PageIndex-1) * @PageSize as varchar) + ' ' + @priKeyName 

                    + ' from ' + @tblName + ' where ' + @strWhere + @strOrder + ') as tmptbl)'

                    + ' and ' + @strWhere

                    + @strOrder

        end

        else  --                

        begin 

            if @strWhere=''

                set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldnames 

                    + ' from ' + @tblName 

                    + ' where not exists (select * from '

                    + '    (select top ' + cast((@PageIndex-1) * @PageSize as varchar) + ' * from ' 

                    + @tblName + @strorder + ') as tmpTable '

                    + ' where tmpTable.' + @priKeyName + ' = ' + @tblName +'.' + @priKeyName + ')'

                    + @strorder

            else

                set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldnames 

                    + ' from ' + @tblName 

                    + ' where not exists (select * from '

                    + '    (select top ' + cast((@PageIndex-1) * @PageSize as varchar) + ' * from ' 

                    + @tblName + ' where ' + @strWhere + @strorder + ') as tmpTable '

                    + ' where tmpTable.' + @priKeyName + ' = ' + @tblName + '.' + @priKeyName + ')'

                    + ' and ' + @strWhere

                    + @strorder

        end

    end



end

--print @strByPage

exec sp_executeSql @strByPage

return

좋은 웹페이지 즐겨찾기