ql 서버 페이지 저장 프로세스

11938 단어
ql 서버 테이블 (보기) 일반 페이지 저장 프로세스
create procedure dbo.proc_getpage
@table_name varchar(500),              --  ,    :tablea a left join tableb b on a.id=b.fk_id
@select_fields varchar(1000)=' * ',    --      
@page_size int=10,                     --   ,   0        ,   
@page_index int=1,                     --  
@sort_field varchar(50)='',            --     
@order_type varchar(10)='desc',        --    , asc|desc
@str_where varchar(max)='',            --    (  :     where)
@row_count int output                  --    
as
begin
    declare @select_sql varchar(max)    --   
    declare @count_sql nvarchar(max)    --        
    declare @order_sql varchar(400)     --     

    if(@str_where!='')
    begin
        set @count_sql=N'select @rowcount=count(*) from ' + @table_name + N' where ' + @str_where
    end
    else
    begin
        set @count_sql=N'select @rowcount=count(*) from ' + @table_name
    end

    set @order_sql=' order by ' + @sort_field + ' ' + @order_type

    if(@page_size=0)
    begin
        if(@str_where!='') 
        begin
            set @select_sql='select ' + @select_fields + ' from ' + @table_name + ' where ' + @str_where + ' ' + @order_sql
        end
        else
        begin
            set @select_sql='select ' + @select_fields + ' from ' + @table_name + ' ' + @order_sql
        end
    end 
    else 
    begin
        set @select_sql='select * from (select row_number() over(' + @order_sql + ') as row_no,' + @select_fields + ' from ' + @table_name
        
        if(@str_where!='')
        begin
            set @select_sql+=' where ' + @str_where
        end

        set @select_sql+=') as temp where row_no between ' + cast(((@page_index-1)*@page_size+1) as varchar) + ' and ' + cast((@page_index*@page_size) as varchar)
    end 
    
    exec sp_executesql @count_sql,N'@rowcount int output',@row_count output
    exec(@select_sql)
end

좋은 웹페이지 즐겨찾기