Assemble a statement string
The mi_exec() function passes the SQL statement to the database server as a statement string, which is a text representation of the SQL statement. To execute a statement with mi_exec(), the statement string must include the entire SQL statement; that is, it cannot contain any input parameters.
You can assemble this statement string in the following
ways:
- If you know all the information at compile time, assemble the
statement as a fixed string.If you know the whole statement structure, you can specify the string itself as the argument to mi_exec(), as the following line shows:
mi_exec(conn, “select company from customer where \ customer_num = 101;", MI_QUERY_BINARY);
- If you do not know all the information about the statement at
compile time, you can use the following features to assemble the statement
string:
- Character variables can hold the identifiers in the SQL statement
(column names or table names) or parts of the statement like the WHERE
clause. They can also contain keywords of the statement.You can then build the SQL statement as a series of string operations, as the following code fragment shows.
mi_string stmt_txt[30]; mi_string fld_name[15]; ... stcopy("select ", stmt_txt); fld_name = obtain_fldname(...); stcat(fld_name, stmt_txt); stcat("from customer where customer_num = 101", stmt_txt); ... mi_exec(conn, stmt_txt, MI_QUERY_BINARY);
- Character variables can hold the identifiers in the SQL statement
(column names or table names) or parts of the statement like the WHERE
clause. They can also contain keywords of the statement.
- If you know what column values the statement specifies, you can
declare program variables to provide column values that are needed
in a WHERE clause or to hold column values that database server returns. The following code fragment shows the SELECT statement of preceding code fragment changed so that it uses a variable to determine the customer number dynamically.
mi_string stmt_txt[30]; mi_integer cust_num; ... stcopy("select company from customer where customer_num = ", stmt_txt); cust_num = obtain_custnum(...); stcat(cust_num, stmt_txt); ... stmt_desc = mi_exec(conn, stmt_txt, MI_QUERY_BINARY);
The statement string can contain multiple SQL statements. Each SQL statement must be terminated with the semicolon (;) symbol.