无忧站长 >

asp用oledb连接操作临时表或存储程序问题

2019-11-01 14:45:28 
下面代码在SQL查询中,正常显示结果,但是在ASP中就显示“对象被关闭时,不允许操作”的错误!
select FInterID into #tmpRight
from SYS_UsrDataRight a, CFG_DataRight b
where b.FInterID = a.FRightID and FTableName = ’Basic_Item’
and a.FUserID = 1
if (select count(1) from #tmpRight) > 0
select FInterID, FItemName, FCreateDate
from Basic_Item
where FInterID in (select FInterID from #tmpRight)
order by FCreateDate DESC
else
select FInterID, FItemName, FCreateDate
from Basic_Item
order by FCreateDate DESC
drop table #tmpRight

解析:在该存储过程调试过程中,发现oledb和odbc存在一个很大的差别,asp向odbc取记录集时,odbc过滤了上面所称的特殊记录集(那种只占位置但不能进行任何操作的记录集——多由create table或insert into产生),而asp向oledb取记录集时,oledb并没有将特殊记录集过滤。
同时,认识到在使用存储过程返回记录集时,在不希望返回记录的地方,应该使用set nocount on禁止存储过程返回 记录集,否则可能会绕很多弯路。
终于明白了为什么绕了这么多弯路:没有想到oledb返回了这么多特殊的记录集
在存储过程中不希望返回记录集前执行set nocount on,要返回记录集时,先执行set nocount off