JDBC Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.11

Creating Procedures with Multiple Queries

This section describes how to create a procedure with multiple queries.

To do more tests on CallableStatement objects, I created another stored procedure with multiple queries:

C:\>\local\mysql\bin\mysql -u Herong -pTopSecret

mysql> USE HerongDB;
Database changed

mysql> -- Changing the command delimiter to '$'
mysql> DELIMITER '$';

mysql> -- Creating the procedure
mysql> CREATE PROCEDURE HeadTail(OUT Size INTEGER)
    -> BEGIN
    ->   SELECT ID, FirstName, LastName, BirthDate, ModTime
    ->     FROM Profile ORDER BY ID LIMIT 0, 10;
    ->   SELECT ID, FirstName, LastName, BirthDate, ModTime
    ->     FROM Profile ORDER BY ID DESC LIMIT 0, 10;
    ->   SELECT COUNT(*) INTO Size FROM Profile;
    -> END$
Query OK, 0 rows affected (0.00 sec)

mysql> -- Testing the procedure
mysql> CALL HeadTail(@Size)$
+-------+-----------+----------+------------+---------------------+
| ID    | FirstName | LastName | BirthDate  | ModTime             |
+-------+-----------+----------+------------+---------------------+
| 80029 | 2265      | 9e559    | 1988-12-31 | 2006-12-31 23:59:59 |
| 80030 | 378       | 48189    | NULL       | 2007-04-01 21:35:14 |
| 80031 | 1a46      | 108f3    | NULL       | 2007-04-01 21:35:14 |
| 80032 | 44f       | c8511    | NULL       | 2007-04-01 21:35:14 |
| 80033 | 1e9a      | 51ca6    | NULL       | 2007-04-01 21:35:14 |
| 80034 | 665       | 6eb12    | NULL       | 2007-04-01 21:35:14 |
| 80035 | 1b97      | 30e6d    | NULL       | 2007-04-01 21:35:14 |
| 80036 | 3d8       | 270d6    | NULL       | 2007-04-01 21:35:14 |
| 80037 | 26a3      | b2290    | NULL       | 2007-04-01 21:35:14 |
| 80038 | 1d65      | d9d6d    | NULL       | 2007-04-01 21:35:14 |
+-------+-----------+----------+------------+---------------------+
10 rows in set (0.00 sec)

+-------+-----------+----------+------------+---------------------+
| ID    | FirstName | LastName | BirthDate  | ModTime             |
+-------+-----------+----------+------------+---------------------+
| 90029 | Herong    | Yang     | 1988-12-31 | 2006-12-31 23:59:59 |
| 90028 | 545       | 728d4    | NULL       | 2007-04-01 21:35:17 |
| 90027 | 169b      | 1f61d    | NULL       | 2007-04-01 21:35:17 |
| 90026 | 2559      | 737b4    | NULL       | 2007-04-01 21:35:17 |
| 90025 | 1db2      | 48b31    | NULL       | 2007-04-01 21:35:17 |
| 90024 | 149       | cdf71    | NULL       | 2007-04-01 21:35:17 |
| 90023 | 12a5      | 8a909    | NULL       | 2007-04-01 21:35:17 |
| 90022 | 1b67      | c6f96    | NULL       | 2007-04-01 21:35:17 |
| 90021 | 12a9      | 20ac8    | NULL       | 2007-04-01 21:35:17 |
| 90020 | 18a4      | 1ffb4    | NULL       | 2007-08-31 21:35:17 |
+-------+-----------+----------+------------+---------------------+
10 rows in set (0.03 sec)

Query OK, 0 rows affected (0.06 sec)

mysql> -- Check the output parameter
mysql> SELECT @Size$
+-------+
| @Size |
+-------+
| 10001 |
+-------+
1 row in set (0.00 sec)

Sections in This Chapter

Overview of CallableStatement Objects

"CREATE PROCEDURE" - Creating a Simple Procedure

Creating Procedures with IN and OUT Parameters

Creating Procedures with INOUT Parameters

Creating Procedures with Multiple Queries

Creating CallableStatement Objects with prepareCall()

Capturing ResultSet with executeQuery()

Creating CallableStatement Objects with Parameters

Common Errors with CallableStatement Parameters

Creating CallableStatement Objects with INOUT Parameters

Retrieving Multiple ResultSet Objects

Executing Stored Procedures without Permission

getProcedures() - Listing Stored Procedures

Dr. Herong Yang, updated in 2007
Creating Procedures with Multiple Queries