我需要从sql server存储过程中读取一些记录集。该过程看起来非常简单,但在阅读过程中,有一些我无法理解的内容
存储过程版本1:
FALSE
这是我读取记录集的c ++代码:
ALTER PROCEDURE [dbo].[sp_cbqueue_pickup_some]
as
create table #tmp
(
id bigint
)
insert #tmp (id)
select top 100 id
from cbqueue
where scheduletime<=GETDATE() and [FetchTime] is null
update CBQueue set fetchtime = GETDATE()
where ID in (select id from #tmp)
select languagesrc,countrysrc,operatorsrc
from cbqueue
where id in (select id from #tmp)
在这种情况下,当代码步骤为此语句时:
try{
_CommandPtr pCommand = NULL;
TBX_TICK curTick = TBX_GET_TICK_PRECISE();
if (mpConn == NULL)
Connect();
else if (((curTick - mLastOperation) / 1000) > 30)
{
Disconnect();
Connect();
}
mLastOperation = curTick;
pCommand.CreateInstance(__uuidof(Command));
pCommand->ActiveConnection = mpConn;
pCommand->CommandText = (_bstr_t)"sp_cbqueue_pickup_some";
pCommand->CommandType = adCmdStoredProc;
pCommand->CommandTimeout = miCommandTimeout;
int n = 0;
_RecordsetPtr pRecordset = NULL;
pRecordset = pCommand->Execute(NULL, NULL, adCmdStoredProc);
while(!(pRecordset->EndOfFile))
{
buffer[n].Index = pRecordset->Fields->GetItem("xxxxxxxxxxxxxxx")->GetValue().intVal;
buffer[n].ANI = (char*)((_bstr_t)(pRecordset->Fields->GetItem("yyyyyyyyyyyyyyyyy")->GetValue()));
buffer[n].DEST = (char*)((_bstr_t)(pRecordset->Fields->GetItem("zzzzzzzzzzzz")->GetValue()));
n++;
if (n >= length)
break;
pRecordset->MoveNext();
}
}
catch (_com_error &e) {
}
程序将跳转到catch块
但如果我将存储过程更改为此格式:
while(!(pRecordset->EndOfFile))
c ++代码不会再发生异常,我花了很多时间才发现这个改变可以删除异常,但我还是不知道是什么原因。 有人能把我搞清楚吗?