EXEC SQL EXECUTE INTO returns nothing

858 views Asked by At

I am using C++ program to connect to a SQL Server and to get the data. 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. But the in above program, the variable mID contains nothing.

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...

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. I think I am doing a silly mistake. But unable to find out. Thanks in Advance.

1

There are 1 answers

0
Malan On

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