【问题标题】:SQL Server Stored Procedure via SOCI/ODBC in C++SQL Server 存储过程通过 C++ 中的 SOCI/ODBC
【发布时间】:2014-02-19 22:43:13
【问题描述】:

我正在使用SOCI library 通过 ODBC 以编程方式与 SQL Server DB 交互。我无法通过存储过程中的输出参数获取值。一些代码(仿照SOCI documentation)...

/*
 * create procedure
 * Dan.soci_proc @id int, @name varchar(32) output
 * as begin
 * set nocount on;
 * select @name = name from Dan.soci_test where id = @id
 * end
 */      
std::string sql = "Dan.soci_proc :id, :name";
std::string name;
int proc_ndx = 1;
soci::procedure proc = (my_soci.get_session().prepare << sql, soci::use(proc_ndx),
                                                           soci::use(name));
std::cout << "\nAttempting to execute stored procedure " << size << " times."
          << std::endl;
for (; proc_ndx < adj_size; ++proc_ndx) {
  try { 
    proc.execute();
    while (proc.fetch())
      std::cout << "Fetched: " << name << std::endl;
  } catch (const soci::odbc_soci_error& e) {
    std::cerr << "Error executing stored procedure." << std::endl;
    std::cerr << e.what() << std::endl;
    std::cerr << e.odbc_error_message() << std::endl;
    return;
  }
}

我的代码没有抛出任何错误或异常,但也没有获取任何内容。我试过用很多不同的方式调用它(普通的exec 语法、ODBC call 语法等),但似乎没有任何效果。我想知道错误是否会回到this from here...

如果省略了输入/输出参数,或者提供了文字 对于参数,驱动丢弃输出值。

不幸的是,SOCI 似乎并不真正支持参数标记,至少据我所知。

我已经通过使用这种语法使代码工作......

std::string sql = "declare @name varchar(32); "
                  "exec Dan.soci_proc :id, @name = @name output; select @name";
...
soci::procedure proc = (my_soci.get_session().prepare << sql, soci::use(proc_ndx),
                                                         soci::into(name));

但这并不理想,原因我认为是显而易见的。

有没有人使用过 SOCI 并对我需要采取哪些不同的措施来使其发挥作用提出了一些意见?

编辑:我收到了来自社会用户论坛/邮件列表的以下回复...

我已经有几年没有使用 SOCI 了,但我确实成功获得了 在 SQL Server 上使用 ODBC 的存储过程,2011 年底,社会 第 2 版(我认为)。

我记得要做到这一点,我必须使用语句而不是过程。

我不得不修改后端以循环调用 SQLMoreResults。

我认为 SOCI 不支持输出参数,我处理了 他们直接使用 ODBC,我也有一个 C++ 层。

但是,由于我是一名刚毕业的大学毕业生(本月才开始我的第一份工作!)并且对 C++ 和数据库应用程序的经验相对缺乏,因此我非常感谢人们能够提供的尽可能多的帮助!例如,关于上述回复 - 我并不完全清楚最后两点。

【问题讨论】:

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


    【解决方案1】:

    问题是我使用的是 SOCI ODBC,它显然不支持存储过程。

    在我最初提出这个问题后的几个月里,我已经实现了自己的 SOCI 后端,存储过程可以完美地工作!

    【讨论】:

    • 是的。需要明确的是,如果有人没有读过您所说的帖子中间...我们仍然可以使用 SOCI 使用以目标 DBMS 的本机方言编写的原始语句在 ODBC 目标上执行 SP。 SOCI 仅在 ODBC 上“不支持”SP,因为它没有为 SP 提供高级别的、与 DBMS 无关的接口——就像它为许多其他功能所做的那样,特别是在其更开发的非 ODBC 后端中。
    猜你喜欢
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    相关资源
    最近更新 更多