临时表操作的一些见解(解决了我在存储过程中使用临时表的困惑)

来源:互联网 发布:云南师范大学知乎 编辑:程序博客网 时间:2024/04/28 06:36

在调试以下存储过程中遇到了很大的困扰(主要是临时表操作):

view plainprint?
  1. CREATE PROCEDURE [c_readtop] @eachrow int=10 AS  
  2. declare @tmpcat varchar(16)  
  3. create table #tmp_result (arid int,cat2 varchar(16),title varchar(100),upday datetime)  
  4. declare rt_cursor cursor  
  5. for select cat2 from category where cat1='电脑手册' and catl=2  
  6. open rt_cursor  
  7. fetch from rt_cursor into @tmpcat  
  8. while @@fetch_status=0  
  9.   Begin  
  10.    set rowcount @eachrow  
  11.    Insert into #tmp_result (arid,cat2,title,upday) Select top 10 arid,cat2,title,upday from article as a left join category as b on a.sortid=b.catid where b.cat1='电脑手册'   
  12.    and b.cat2=@tmpcat order by upday desc  
  13.    fetch from rt_cursor into @tmpcat  
  14.   End  
  15. select * from #tmp_result  
  16. drop table #tmp_result  
  17. close rt_cursor  
  18. deallocate rt_cursor  


此存储过程的作用是取出每个分类的最新10条记录。
出现的错误信息是(一旦操作返回的记录集时就出现):
ADODB.Recordset 错误 '800a0e78' 

The operation requested by the application is not allowed if the object is closed. 

   此存储过程能在qa中正常运行且能得到正确结果,使用odbc连接数据库的话,也能得到正确的结果。于是首先怀疑oledb方式连接没能返回记录集。进行了下面的调试:
(一)加调试标记,在调用记录集前用set rs=rs.nextrecordset测试是不是命中返回的记录集……
(二)由于该过存原来是另一个过程的一部分,怀疑存储过程中有些语句不能同时使用,于是将该过程分离成一个独立的存储过程,错误依旧。
(三)怀疑调用该过程的Asp有问题,于是重做一个只是调用该存储过程的Asp程序,错误依旧。
(四)将连接方式改为odbc方式连接(建dsn,设sql server的login ID,设权限),该错误消失。重新使用oledb连接,错误依旧。
(五)怀疑对临时表的数据插入有问题,取消去临时表插入数据,能返回一个空的记录集。
(六)经Bigeagle提示,将临时表建在临时数据库tempdb上,错误依旧

(七)把存储过程中的drop table去掉,在qa中运行该存储过程,观察临时表的生成情况,发现临时表正确生成且有正确的数据插入,百思不得其解,数据输出到哪了?
(八)经Bigeagle提示create table一句返回了记录集,于是重新在输出记录集前使用多个set rs=rs.nextrecordset(最多放上了4个),错误提示依旧。
(九)怀疑临时表操作有问题,将临时表改为固定表,不插入数据时返回空记录集,插入记录时仍然提示错误。在记录集输出前先执行一个或多个set rs=rs.nextrecordset,终于有一次没有提示出错(检测到rs.eof为false),于是才恍然大悟——不但是create table返回了记录集,而且连insert into语句也返回了记录集,不过该记录集得一种特别的记录集(没有字段,不能对该记录集进行任何操作——连检测rs.eof都不允许),我在此将它称为特殊的记录集,方便下面引用。
(十)知道了问题的症结,就马上解决了,在存储过程中不希望返回记录集前执行set nocount on,要返回记录集时,先执行set nocount off。

也就是改成:

view plainprint?
  1. CREATE PROCEDURE [c_readtop] @eachrow int=10 AS  
  2. declare @tmpcat varchar(16)  
  3. set nocount on  
  4. create table #tmp_result (arid int,cat2 varchar(16),title varchar(100),upday datetime)  
  5. declare rt_cursor cursor  
  6. for select cat2 from category where cat1='电脑手册' and catl=2  
  7. open rt_cursor  
  8. fetch from rt_cursor into @tmpcat  
  9. while @@fetch_status=0  
  10.   Begin  
  11.    set rowcount @eachrow  
  12.    Insert into #tmp_result (arid,cat2,title,upday) Select top 10 arid,cat2,title,upday from article as a left join category as b on a.sortid=b.catid where b.cat1='电脑手册'  
  13.  and b.cat2=@tmpcat order by upday desc  
  14.    fetch from rt_cursor into @tmpcat  
  15.   End  
  16. set nocount off  
  17. select * from #tmp_result  
  18. drop table #tmp_result  
  19. close rt_cursor  
  20. deallocate rt_cursor  


问题解决。

   在该存储过程调试过程中,发现oledb和odbc存在一个很大的差别,asp向odbc取记录集时,odbc过滤了上面所称的特殊记录集(那种只占位置但不能进行任何操作的记录集——多由create table或insert into产生),而asp向oledb取记录集时,oledb并没有将特殊记录集过滤。
   同时,认识到在使用存储过程返回记录集时,在不希望返回记录的地方,应该使用set nocount on禁止存储过程返回记录集,否则可能会绕很多弯路。
   终于明白了为什么绕了这么多弯路:没有想到oledb返回了这么多特殊的记录集(还是由一个循环产生的,该循环执行次数5、6次),怪不得在取记录集前虽然执行了set rs=rs.nextrecordset,但终因数据不够多而未能发现错误症结所在。

 

原创粉丝点击