【发布时间】: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