Sometimes all you want is to execute a single SQL query and return the results to the calling application. In these simple cases, writing the necessary Java code can be tedious. So VoltDB provides a shortcut.
For very simple stored procedures that execute a single SQL query and return the results, you can define the entire stored procedure as part of the database schema. Normally, the schema contains entries that identify each of the stored procedures, like so:
CREATE PROCEDURE FROM CLASS procedures.MakeReservation; CREATE PROCEDURE FROM CLASS procedures.CancelReservation;
The CREATE PROCEDURE statement specifies the class name of the Java procedure you write. However, to create procedures without writing any Java, you can simply insert the SQL query in the AS clause:
CREATE PROCEDURE CountReservations AS SELECT COUNT(*) FROM RESERVATION;
When you include the SQL query in the CREATE PROCEDURE AS statement, VoltDB creates the procedure when you build your application (as described in Section 5.3, “Building the Application Catalog”). Note that you must specify a unique class name for the procedure, which is unique among all stored procedures, including both those declared in the schema and those created as Java classes.
It is also possible to pass arguments to the SQL query in simple stored procedures. If you use the question mark placeholder in the SQL, any additional arguments you pass through the callProcedure method are used to replace the placeholders, in their respective order. For example, the following simple stored procedure expects to receive three additional parameters:
CREATE PROCEDURE MyReservationsByTrip AS SELECT R.RESERVEID, F.FLIGHTID, F.DEPARTTIME FROM RESERVATION AS R, FLIGHT AS F WHERE R.CUSTOMERID = ? AND R.FLIGHTID = F.FLIGHTID AND F.ORIGIN=? AND F.DESTINATION=?;
Finally, you can also specify whether the simple procedure is single-partitioned or not. By default, simple stored procedures are assumed to be multi-partitioned. But if your procedure is single-partitioned, you can specify the partitioning information in a PARTITION PROCEDURE statement. In the following example, the stored procedure is partitioned on the FLIGHTID column of the RESERVATION table using the first parameter as the partitioning key.
CREATE PROCEDURE FetchReservations AS SELECT * FROM RESERVATION WHERE FLIGHTID=?; PARTITION PROCEDURE FetchReservations ON TABLE Reservation COLUMN flightid;