日期:2014-05-19  浏览次数:20654 次

无法移除"'tempTablePage1'",因为它在系统目录中不存在



CREATE   PROCEDURE   spSelectListDynamicPaged2    
(    
  @SelectStatement   nvarchar(4000),    
  @FromStatement   nvarchar(2000),    
  @WhereStatement   nvarchar(4000),    
  @OrderByExpression   nvarchar(500),    
  --add   @AscOrDesc    
  @AscOrDesc   nvarchar(10),    
  @RecordCount   int,    
  @PageSize   int,      
  @PageIndex   int,      
  @DoCount   bit    
)    
   
AS    
   
SET   NOCOUNT   ON    
   
IF(@DoCount=1)    
--if   do   count,   return   the   count   simply    
   
  EXEC( 'SELECT   count(*)     FROM   '+@FromStatement+ '   WHERE   1=1   '+@WhereStatement)    
ELSE    
BEGIN    
   
declare   @nCount   as   int
declare   @nTotalPage   As   int
declare   @sSelectCopy   As   nvarchar(2000)
declare   @TempTable   As   nvarchar(100)

if   isnull(@WhereStatement, ' ')   =   ' '  
begin
set   @WhereStatement   =   '1=1 '
end
else
begin
set   @WhereStatement   =   right(ltrim(@WhereStatement),len(ltrim(@WhereStatement))-3)
end


if   isnull(@OrderByExpression, ' ')   =   ' '
begin
set   @OrderByExpression   =   '1 '
end

if   exists   (select   1   from   sysobjects   where   id   =   object_id( 'tempTablePage1 ')   and   type   =   'U ')
begin
drop   table   tempTablePage1
end

set   @SelectStatement   =     'select   top   100000000   '   +   @SelectStatement


exec   ( 'select   a.*   ,   identity(int,1,1)   as   NumberIndex   into   tempTablePage1   from   ( '   +   @SelectStatement   +   '   from   '   +   @FromStatement   +   '   where   '   +   @WhereStatement   +   '   order   by   '   +   @OrderByExpression   +   '   '   +   @AscOrDesc   + ')   as   a ')

set   @nCount   =   (select   max(NumberIndex)   from   tempTablePage1)
if   @nCount   %   @PageSize   >   0
begin
set   @nTotalPage   =   @nCount   /   @PageSize   +   1
end
else
begin
set   @nTotalPage   =   @nCount   /   @PageSize
end

if   @PageIndex   <=   0  
begin
set   @PageIndex   =   1
end
else   if   @PageIndex   >   @nTotalPage
begin
set   @PageIndex   =   @nTotalPage
end


if   @PageIndex   <   @nTotalPage
begin
sele