I have to generate a c program that can generate a dynamic update sql and execute it. The table name, no of columns to be updated and columns in where clause are known only during run time.So i split my program into 2 functions:
1)CheckTableExists - checks whether the table provided by user exists in db. Successfully implemented.
2)UpdateFunc - generates the sql string from data provided by user and executes
the query.
I am having issues in second function.I accept no of columns,names and values to be updated from user. I then use a for loop and append the names and values to my sql string:
sqlstring = Update tabname set colname = colval,colname = colval where
Then i accept where clause data and append it to my string. So my final string is:
sqlstring = Update tabname set colname = colval,colname = colval where whcolname = whcolval
But when i execute it my program hangs.Can somebody just tell me what should be the
ideal approach to execute second function.
strSQL
will be a valid SQL statement without placeholders for binding. This is suitable for queries with numeric and varchar2 variables. For binary you may have to implement functions say bin2hex() and hex2bin() in Oracle.
A simple function like:
int Exec_Query(sql_context sqlCtx, char *strSQL)
{
// struct sqlca sqlca;
/* Sanity checks for sqlCtx and strSQL */
EXEC SQL WHENEVER SQLERROR GOTO QueryErr;
/* Set the context */
EXEC SQL CONTEXT USE :sqlCtx;
/* Execute the query */
EXEC SQL EXECUTE IMMEDIATE :strSQL;
EXEC SQL COMMIT;
return 0;
QueryErr:
/* printf("Err: Failure in Executing Query\n"); */
/* printf("Debug: <%s>",sqlca.sqlerrm.sqlerrmc); */
return -1;
}
Which through a Pro*C compiler will give a working C file.