완벽한 페이지 나누기 스토리지 프로세스

4660 단어 procedure
자세히 보기

CREATE   procedure   main_table_pwqzc  
  (@pagesize   int,  
  @pageindex   int,  
  @docount   bit,  
  @this_id   int)  
  as  
  if(@docount=1)  
  begin  
  select   count(id)   from   luntan   where   this_id=@this_id  
  end  
  else  
  begin  
  declare   @PageLowerBound   int  
  declare   @PageUpperBound   int  
  set   @PageLowerBound=(@pageindex-1)*@pagesize  
  set   @PageUpperBound=@PageLowerBound+@pagesize  
  create   table   #pageindex(id   int   identity(1,1)   not   null,nid   int)  
  set   rowcount   @PageUpperBound  
  insert   into   #pageindex(nid)  
  select   id   from   luntan   where   this_id=@this_id   order   by   reply_time   desc  
  select   O.*  
  from   luntan   O,#pageindex   p  
  where   O.id=p.nid   and   p.id>@PageLowerBound   and   p.id<=@PageUpperBound   order   by   p.id  
  end  
  GO 

데이터베이스를 읽을 때 페이지를 나누는 저장 프로세스 (sql 서버 버전)

        
Create  PROCEDURE ListPage(
@tblName  nvarchar(200),           ----            
@fldName  nvarchar(200) = '*',          ----        
@pageSize int = 10,                    ----         
@page  int = 1,           ----         
@pageCount int = 1 output,          ----           
@Counts int = 1 output,           ----       
@fldSort nvarchar(100) = null,          ----         
@Sort  bit = 0,           ----    ,0   ,1   
@strCondition nvarchar(200) = null, ----    ,  where
@ID  nvarchar(50)  ----     
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar(1000)  ----       SQL  
Declare @strTmp nvarchar(1000)  ----               
Declare @strID  nvarchar(1000)  ----           ID     
Declare @sqlSort nvarchar(200)  ----           
Declare @intCounts int   ----       
Declare @BeginID int   ----   ID
Declare @EndID   int   ----   ID

--------        ---------
if @Sort=0  --  
begin
   if not(@fldSort is null)
  set @sqlSort = ' Order by ' + @fldSort 
   else
  set @sqlSort = ' Order by ' + @ID 
end
else   --  
begin
 if not(@fldSort is null)
  set @sqlSort = ' Order by ' + @fldSort + ' DESC'  
 else
  set @sqlSort = ' Order by ' + @ID + ' DESC '
end


--------      --------
--  @strTmp            
if @strCondition is null --        
 begin
 set @sqlTmp =  @fldName + ' From ' + @tblName
 set @strTmp = 'select @Counts=Count(' + @ID + ') FROM '+@tblName
 set @strID = ' From ' + @tblName
 end
else
 begin
 set @sqlTmp = + @fldName + 'From ' + @tblName + ' where ' + @strCondition
 set @strTmp = 'select @Counts=Count(' + @ID + ') FROM '+@tblName + ' where ' + @strCondition
 set @strID = ' From ' + @tblName + ' where ' + @strCondition
 end
 


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

--      
if @Counts <= @pageSize
 set @pageCount = 1
else
 set @pageCount = (@Counts / @pageSize) + 1


--         
if @page = 1
 set @intCounts = @pageSize
else
begin
 set @intCounts = (@page-1) * @pageSize + 1
end

-----              ID
set @strID = 'select @BeginID=' + @ID + ' ' + @strID

set @intCounts = @intCounts - @pageSize + 1
set rowcount  @intCounts
exec sp_executesql @strID,N'@BeginID int out ',@BeginID out

-----               ID
set @intCounts = @intCounts + @pageSize - 1
print @intCounts
set rowcount  @intCounts
exec sp_executesql @strID,N'@BeginID int out ',@EndID out


------      -----
set rowcount  0
SET NOCOUNT OFF

------      -----
if @strCondition is null
 set @strTmp = 'select ' + @sqlTmp + ' where ' + @ID + ' between ' + str(@BeginID) + ' and ' + str(@EndID)
else
 set @strTmp = 'select ' + @sqlTmp + ' where ' + @ID +' (between ' + str(@BeginID) + ' and ' + str(@EndID) + ')  and ' + @strCondition

if not(@sqlSort is null)
set @strTmp = @strTmp + @sqlSort
exec sp_executesql @strTmp

GO


좋은 웹페이지 즐겨찾기