首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

分页存储过程有关问题

2012-01-24 
分页存储过程问题http://community.csdn.net/Expert/topic/5537/5537878.xml?temp.4858362http://communi

分页存储过程问题
http://community.csdn.net/Expert/topic/5537/5537878.xml?temp=.4858362
http://community.csdn.net/Expert/topic/5540/5540718.xml?temp=.3774225
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
select   *   from   tempTablePage1   where   NumberIndex   between   (@PageIndex   -   1)*@PageSize   +   1   and   @PageIndex*@PageSize


end
else   if   @PageIndex   =   @nTotalPage
begin  
select   *   from   tempTablePage1   where   NumberIndex   between   (@PageIndex-1)*@PageSize   +   1   and   @nCount  
end

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

end
GO
---------------------------
用了上面的这个分页存储过程,发现偶尔出现
无法移除" 'tempTablePage1 '",因为它在系统目录中不存在
这种错误.发生的频率十分少.但是不知道是什么bug?
------------------------------------

按照:wangtiecheng(不知不为过,不学就是错!)  
exec   ( 'select   a.*   ,   identity(int,1,1)   as   NumberIndex   into   tempTablePage1   from   ( '   +   @SelectStatement   +   '   from   '   +   @FromStatement   +   '   where   '   +   @WhereStatement   +   '   order   by   '   +   @OrderByExpression   +   '   '   +   @AscOrDesc   + ')   as   a ')

这里面的tempTablePage1   替换为临时表#tempTablePage1   ,并把之后的所有SQL都写到这个Exec   的动态脚本中。因为在Exec动态创建的临时表,在外部无法访问。

我改了之后运行错误,请谁帮忙彻底的改一下.多谢


[解决办法]
wgsasd311说的原因应该是对的。但是解决办法按照我之前在项目组做的方法是:
1。用全局临时表
2。为了不会让不同的进程共用一个临时表导致冲突,我通过动态生成临时表的表名来实现。
表名是通过GUID获得。

方法如下:
--获得表名
declare @TableName varchar(40)
select @TableName = cast(newid() as nvarchar(40))
set @TableName = replace(@TableName, '- ', ' ')
set @TableName = '## ' + @TableName

--动态插入表数据
exec( 'select * into ' + @TableName + ' from tbl_Performance_Appraise ')

--查看结果
--exec( 'select * from ' + @TableName)

--通过新生成的这个表,执行分页操作,就按照你自己的方法

--最后帮刚才新建的全局临时表删除
exec( 'drop table ' + @TableName)
[解决办法]
楼上的应该可以,采用临时表应该相对可靠。
动态的全局临时表也可行(呵呵,这个用法第一次遇到,有点新意)
我用楼上的。
不过楼主的SQL写法有些地方过于偷懒,建议标准点写,影响执行效率

热点排行