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

6859 단어 SQLSERVER
1. 리스트 조회 페이지 저장 프로세스
CREATE PROCEDURE [dbo].[prcPageResult]
(
	@currPage int = 1,                         --      ( Top currPage)
	@showColumn varchar(2000) = '*',           --        (  column1,column2,......)
	@tabName varchar(2000),                    --        (  from table_name)
	@strCondition varchar(2000) = '',          --     (  where condition......)    where   
	@ascColumn varchar(100) = '',              --       (  order by column asc/desc)
	@bitOrderType bit = 0,                     --      (0   ,1   )
	@pkColumn varchar(50) = '',                --    
	@pageSize int = 20,                        --    
	@GetCount int =0,						   --       ,0       ,  0    
	@Count int = 0 output					   --  
)
AS
BEGIN --       
--                --
DECLARE @strTemp varchar(1000)
DECLARE @strSql varchar(4000)             --            
DECLARE @strOrderType varchar(1000)       --       (order by column asc  order by column desc)

BEGIN
IF @bitOrderType = 1   -- bitOrderType=1     
BEGIN
    SET @strOrderType = ' ORDER BY '+@ascColumn+' DESC'
    SET @strTemp = '(SELECT max'
END

IF @currPage = 1    --       
BEGIN
    IF @strCondition != ''
        SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@showColumn+' FROM '+@tabName+
            ' WHERE '+@strCondition+@strOrderType
    ELSE
        SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@showColumn+' FROM '+@tabName+@strOrderType
END

ELSE    --    
BEGIN
    IF @strCondition !=''
        SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@showColumn+' FROM '+@tabName+
        ' WHERE '+@strCondition+' AND '+@pkColumn+@strTemp+'('+@pkColumn+')'+' FROM (SELECT TOP '+STR((@currPage-1)*@pageSize)+
        ' '+@pkColumn+' FROM '+@tabName+@strOrderType+') AS TabTemp)'+@strOrderType
    ELSE
        SET @strSql = 'SELECT TOP '+STR(@pageSize)+' '+@showColumn+' FROM '+@tabName+
        ' WHERE '+@pkColumn+@strTemp+'('+@pkColumn+')'+' FROM (SELECT TOP '+STR((@currPage-1)*@pageSize)+' '+@pkColumn+
        ' FROM '+@tabName+@strOrderType+') AS TabTemp)'+@strOrderType
END

END
	EXEC (@strSql)
	/*
      @GetCount=0,      (                ,     ,
               ,         ,       ,select count(*)       )
    */
    if(@GetCount=0)
      begin
        declare @sql nvarchar(max)
        set @sql='SELECT @i=COUNT(*) FROM '+@tabName+' WHERE '+@strCondition    
        execute sp_executesql @sql,N'@i int out',@Count OUT--      
      end
    else
        set @Count=@GetCount
END  --       
------------------------------------------------

GO

2. 다중 테이블 조회 페이지 저장 프로세스
/*
            (    )
--    1
declare @Count int
exec [proc_DataPagination] 'SL_Article a,SL_User u','u.RealName,a.*','a.UserId=u.UserId','a.ID',1,20,0,@Count output
select @Count
--    2
declare @Count int
exec proc_DataPagination 'SL_LANAndWANPermissionLog l left join SL_Plate p on l.PlateId=p.PlateId left join SL_Admin a on l.AddUserId=a.UserId','l.*,p.PlateName,a.RealName as AddUserRealName','','a.ID',1,20,0,@Count output
select @Count
*/
/*  :    ,           ,          ,     */
CREATE PROCEDURE [dbo].[proc_DataPagination]
(
    @Table nvarchar(1000),--  ,      
    @Fields varchar(2000) = N'*',--   
    @Where nvarchar(4000) = N'',--where  ,    where
    @OrderBy varchar(1000) = N'',--    ,    order by
    @CurrentPage int = 1, --   , 1  ,  0
    @PageSize int = 10,--         
    @GetCount int =0,--       ,0       ,  0    
    @Count int = 0 output--  
)
AS
BEGIN
    SET NOCOUNT ON
    --        ,      
    if @OrderBy is null or @OrderBy=''
    begin
        declare @tempTable varchar(200)
        set @Table=ltrim(rtrim(@Table))--         
        --              ,              
        if charindex(' on ',@Table)>0
            set @tempTable=substring(@Table,0,charindex(' ',@Table))
        else if charindex(',',@Table)>0
            begin
                set @tempTable=substring(@Table,0,charindex(',',@Table))
                --      Article a,User u
                if(charindex(' ',@tempTable)>0)
                    set @tempTable=substring(@tempTable,0,charindex(' ',@tempTable))
            end
        else
            begin
                if(charindex(' ',@Table)>0)--SL_Article a            
                    set @tempTable=substring(@Table,0,charindex(' ',@Table))
                else
                    set @tempTable=@Table--    
            end

        --       
        if not exists(select * from sysobjects where [name]=@tempTable)
          begin
            raiserror('   %s   ',12,12,@tempTable)
            return
          end    

        --      
        declare @objectid int;Set @objectid=object_id(@tempTable)
        select top 1 @OrderBy=col_name(@objectid,colid) from sysobjects as o 
            Inner Join sysindexes as i On i.name=o.name 
            Inner Join sysindexkeys as k On k.indid=i.indid 
        Where o.xtype = 'PK' and parent_obj=@objectid and k.id=@objectid
        --      ,   
        if @OrderBy is null or @OrderBy = ''
          begin
            raiserror('%s   @OrderBy       ',12,12,@tempTable)
            return
          end
    end

    --    
    if @PageSize < 1
       set @PageSize=10

    --     
    if @CurrentPage < 1
        set @CurrentPage = 1

    --    
    if @Fields is null or @Fields = ''
        set @Fields='*'

    --    
    if @Where is null or @Where=''
        set @Where=''
    else
        set @Where=' WHERE '+@Where

    /*      */
    declare @startRow varchar(50),@endRow varchar(50)
    set @startRow = cast(((@CurrentPage - 1)*@PageSize + 1) as nvarchar(50))
    set @endRow = cast(@CurrentPage*@PageSize as nvarchar(50))
    
    exec
    (
        'SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY '+@OrderBy+') AS rownumber,'+@Fields+
        ' FROM '+@Table+@Where+') AS tempdt WHERE rownumber BETWEEN '+@startRow+' AND '+@endRow
    )

    /*
      @GetCount=0,      (                ,     ,
               ,         ,       ,select count(*)       )
    */
    if(@GetCount=0)
      begin
        declare @sql nvarchar(max)
        set @sql='SELECT @i=COUNT(*) FROM '+@Table+@Where    
        execute sp_executesql @sql,N'@i int out',@Count OUT--      
      end
    else
        set @Count=@GetCount
END
GO

좋은 웹페이지 즐겨찾기