ql 서버 저장 프로세스 페이지 조회
                                            
 6859 단어  SQLSERVER
                    
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  --       
------------------------------------------------
GO2. 다중 테이블 조회 페이지 저장 프로세스
/*
            (    )
--    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이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
SqlServer 데 이 터 를 Oracle 로 이전 하 는 방법 요약1. BCP 명령 을 사용 하여 csv 파일 로 Sqlserver 의 데 이 터 를 내 보 냅 니 다.우 리 는 이 명령 들 을 cmd 명령 으로 쓸 수 있 습 니 다. 문 구 는 다음 과 같 습 니 다. sql 문...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.