SQLSTATE 24000 - 无效的游标状态ODBC VS c ++

时间:2016-03-09 12:08:09

标签: c++ sql-server stored-procedures odbc

我有一个相当简单的c ++应用程序,它使用ODBC连接到使用存储过程填充的SQL Server实例。

我使用wstring对象的行来构建一个查询,然后将该查询传递给存储过程。如果我运行一次,一切正常 - 但是,我希望能够遍历相当大量的代码(3000+ excel行),并且当我这样做时,标题中提到的游标错误发生。

这是存储过程:

dist/

这是连接器:

GO
CREATE PROCEDURE s_addHistorical
    @Symbol nchar(10),@Date datetime,
    @Open decimal(8,2),@Close decimal(8,2),@MinPrice decimal(8,2),
    @MaxPrice decimal(8,2),@Volume int
AS 
    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
    BEGIN TRANSACTION
    MERGE HistoricalStock WITH (UPDLOCK) AS myTarget
        USING (SELECT @Symbol AS Symbol,
        @Date AS Date, @Open AS [Open], @Close AS [Close],
        @MinPrice AS MinPrice, @MaxPrice AS MaxPrice,@Volume AS Volume) AS mySource
        ON mySource.Symbol = myTarget.Symbol AND mySource.Date = myTarget.Date
        WHEN MATCHED 
            THEN UPDATE 
                SET [Open] = mySource.[Open], [Close] = mySource.[Close],
                MinPrice = mySource.MinPrice, MaxPrice = mySource.MaxPrice, Volume = mySource.Volume            
        WHEN NOT MATCHED
            THEN
                INSERT(Symbol,Date,[Open],[Close],MinPrice,MaxPrice,Volume)
                VALUES(@Symbol,@Date,@Open,@Close,@MinPrice,@MaxPrice,@Volume);
    COMMIT 
GO

我一直在寻找谷歌和SO,但我似乎无法找到任何可用于我当前问题的问题。他们中的大多数似乎围绕存储过程回送某些提取,而我的SP只是插入/更新。

任何形式的帮助都会受到极大的关注。 :)

任何?

1 个答案:

答案 0 :(得分:1)

您需要调用SQLCloseCursor来释放光标。

将您的代码更改为:

while (std::getline(stream, line)) {
    vector<wstring> vHistorical = parseData(L"" + line, ',');
    std::wstring SQL = buildQuery(vHistorical);

    if (SQL_SUCCESS != SQLExecDirectW(stmt, const_cast<SQLWCHAR*>(SQL.c_str()), SQL_NTS)) {
        show_error(SQL_HANDLE_STMT, stmt);
    }
    // Close Cursor before next iteration starts:
    SQLRETURN closeCursRet = SQLCLoseCursor(stmt);
    if(!SQL_SUCCEEDED(closeCursRet))
    {
        // maybe add some handling for the case that closing failed.
    }
}

请参阅:https://msdn.microsoft.com/en-us/library/ms709301%28v=vs.85%29.aspx