@AdHoc — Executes an SQL statement specified at runtime.
@AdHoc String SQL-statement [, statement-parameter ... ]
The @AdHoc system procedure lets you perform arbitrary SQL statements on a running VoltDB database.
You can execute multiple SQL statements — either queries or data definition language (DDL) statements — in a single call to @AdHoc by separating the individual statements with semicolons. When you do this, the statements are performed as a single transaction. That is, the statements all succeed as a group or they all roll back if any of them fail. You cannot mix SQL queries and DDL in a single @AdHoc call.
You can use question marks in the SQL statement as placeholders that are replaced by parameters you provide as additional arguments to the call. For example:
sql = "SELECT * FROM Products WHERE partnumber=? AND vendor=?;" results = client.callProcedure("@AdHoc",sql, productid, vendorid);
Performance of ad hoc queries is optimized, where possible. However, it is important to note that ad hoc queries are not pre-compiled, like queries in stored procedures. Therefore, use of stored procedures is recommended over @AdHoc for frequent, repetitive, or performance-sensitive queries.
Returns one VoltTable for each statement, with as many rows as there are records returned by the statement. The column names and datatypes match the names and datatypes of the fields returned by the query.
The following program example uses @AdHoc to execute an SQL SELECT statement and display the number of reservations for a specific customer in the flight reservation database.
try { VoltTable[] results = client.callProcedure("@AdHoc", "SELECT COUNT(*) FROM RESERVATION " + "WHERE CUSTOMERID=" + custid).getResults(); System.out.printf("%d reservations found.\n", results[0].fetchRow(0).getLong(0)); } catch (Exception e) { e.printStackTrace(); }
Note that you do not need to explicitly invoke @AdHoc when using sqlcmd. You can type your statement directly into the sqlcmd prompt, like so:
$ sqlcmd 1> SELECT COUNT(*) FROM RESERVATION WHERE CUSTOMERID=12345;