SQL 2005 存储过程结果集的 ODBC 执行/获取无法使用表 @variable

发布于 2024-08-21 12:53:16 字数 1116 浏览 6 评论 0原文

我正在针对 SQL Server 2005(本机客户端)使用 ODBC 和 C++。
我有以下简单的测试存储过程,它返回两行常量值:

CREATE PROCEDURE usp_testme AS BEGIN  
   declare @details table( one int, two int, three int, four int )  
   insert @details SELECT 1 one, 2 two, 3 three, 4 four UNION SELECT 5, 6, 7, 8  
   select one, two, three, four from @details  
END  

ODBC 代码如下所示(简化):

SQLExecDirect( m_hstmt, (SQLCHAR *)"{CALL usp_testme}", SQL_NTS );
SQLFetch( m_hstmt );   

SQLExecDirect() 调用返回以下信息:

INFO: 16954 : 01000 : [Microsoft etc]Executing SQL directly; no cursor  

然后 SQLFetch() 调用给出:

INFO: 0 : 24000 : [Microsoft etc]Invalid cursor state  

ODBC 参考文档讲述的内容我的问题是我没有结果集。
因此,我将存储过程更改为以下内容(例如,不使用表变量):

CREATE PROCEDURE usp_testme AS BEGIN  
 SELECT 1 one, 2 two, 3 three, 4 four UNION SELECT 5, 6, 7, 8  
END  

瞧,它有效。当然这是一个简单的测试过程,我需要执行的真正的SQL更复杂并且需要表变量,无需赘述。但是为什么不能根据第一个 SP 的结果集创建 (ODBC) 游标呢?

有谁知道如何让它发挥作用? 顺便说一句,我在环境句柄上断言 ODBC v3。我在连接句柄上尝试了不同的光标属性,但无济于事。

非常感谢帮助!

I'm using ODBC and C++ against SQL Server 2005 (native client).
I have the following simple test stored procedure that returns two rows of constant values:

CREATE PROCEDURE usp_testme AS BEGIN  
   declare @details table( one int, two int, three int, four int )  
   insert @details SELECT 1 one, 2 two, 3 three, 4 four UNION SELECT 5, 6, 7, 8  
   select one, two, three, four from @details  
END  

ODBC Code looks like this (simplified):

SQLExecDirect( m_hstmt, (SQLCHAR *)"{CALL usp_testme}", SQL_NTS );
SQLFetch( m_hstmt );   

The SQLExecDirect() call returns the following information:

INFO: 16954 : 01000 : [Microsoft etc]Executing SQL directly; no cursor  

and the SQLFetch() call then gives:

INFO: 0 : 24000 : [Microsoft etc]Invalid cursor state  

What the ODBC reference documentation tells me is that I dont have a result set.
So I change then stored procedure to the following instead (e.g. not using a table variable):

CREATE PROCEDURE usp_testme AS BEGIN  
 SELECT 1 one, 2 two, 3 three, 4 four UNION SELECT 5, 6, 7, 8  
END  

and voila, it works. Of course this is a simple test procedure, the real SQL I need executed is more complex and requires the table variable, no need to go into that. But why cannot an (ODBC) cursor be created against the result set from the first SP?

Does anyone know how to get this to work?
Btw, i'm asserting ODBC v3 on the environment handle. I've tried different cursor attributes on the connection handle but to no avail.

Help is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请恋爱 2024-08-28 12:53:16

问题是语句上的可滚动游标设置,更改为不可滚动游标解决了问题。

The problem was the scrollable cursor setting on the statement, changing to a nonscrollable cursor solved the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文