简体   繁体   English

EXEC SQL EXECUTE INTO不返回任何内容

[英]EXEC SQL EXECUTE INTO returns nothing

I am using C++ program to connect to a SQL Server and to get the data. 我正在使用C ++程序连接到SQL Server并获取数据。 Below is the code... 以下是代码......

string temp = "SELECT WERT FROM AM_TABLE WHERE CD=\'" + prefix + "\'"; // prefix = BADGUN
strcpy((char*)stmt.arr, temp.c_str());
stmt.len = strlen((char*) stmt.arr );
stmt.arr[stmt.len]= '\0';

EXEC SQL DECLARE SELECTDATA STATEMENT;
EXEC SQL PREPARE SELECTDATA FROM :stmt;

cout << "Statement is :" << (char*)stmt.arr << endl;
// SELECT WERT FROM AM_TABLE WHERE CD='BADGUN'

if( sqlca.sqlcode != 0 )
{
    cout << "Code: " << sqlca.sqlcode << " Message: "<< (char*) sqlca.sqlerrm.sqlerrmc << endl;
}
EXEC SQL EXECUTE SELECTDATA INTO:mID;

if( sqlca.sqlcode != 0 )
{
    cout << "Code: " << sqlca.sqlcode << " Message: "<< (char*) sqlca.sqlerrm.sqlerrmc << endl;
}

cout << "mID: " << (char*)mID.arr) << endl;

Everything is executed properly without any errors. 一切都正确执行,没有任何错误。 If I execute the query manually, the query is returning an ID as expected. 如果我手动执行查询,则查询将按预期返回ID。 But the in above program, the variable mID contains nothing. 但是在上面的程序中,变量mID什么mID包含。

Am I doing any mistake here? 我在这里犯了什么错吗?

EDIT 编辑

I also tried... 我也试过......

string temp = "\'" + prefix + "\'";
strcpy((char*)stmt.arr, temp.c_str());
stmt.len = strlen((char*) stmt.arr );
stmt.arr[stmt.len]= '\0';
EXEC SQL SELECT WERT INTO:mID
    FROM AM_TABLE 
    WHERE CD=:stmt;
if( sqlca.sqlcode != 0 )
{
    cout << "Code: " << sqlca.sqlcode << " Message: "<< (char*) sqlca.sqlerrm.sqlerrmc << endl;
}  

But no success... 但没有成功......

But When I hard code the value for stmt like... 但当我硬编码stmt的值时...

EXEC SQL SELECT WERT INTO:mID
    FROM AM_TABLE 
    WHERE CD='BADGUN';
if( sqlca.sqlcode != 0 )
{
    cout << "Code: " << sqlca.sqlcode << " Message: "<< (char*) sqlca.sqlerrm.sqlerrmc << endl;
}  

I am able to get the value. 我能够获得价值。 I can't hard code here, because that stmt variable value is dynamic. 我不能在这里硬编码,因为该stmt变量值是动态的。 I think I am doing a silly mistake. 我想我犯了一个愚蠢的错误。 But unable to find out. 但无法找到答案。 Thanks in Advance. 提前致谢。

I think the problem is with the statement 我认为问题在于声明

EXEC SQL EXECUTE SELECTDATA INTO:mID;

which should be replaced by 哪个应该被替换

EXEC SQL SELECT DATA INTO :mID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM