@@ -141,16 +141,19 @@ protected void doClean() throws SQLException {
141141 jdbcTemplate .execute (dropStatement );
142142 }
143143
144- // procedures
145- for (String dropStatement : generateDropStatementsForProcedures ()) {
146- jdbcTemplate .execute (dropStatement );
147- }
148-
149144 // triggers
150145 for (String dropStatement : generateDropStatementsForTriggers ()) {
151146 jdbcTemplate .execute (dropStatement );
152147 }
153148
149+ // drop procedures by iteratively dropping procedues without dependents (until no procedures are remaining)
150+ do {
151+ dropStatements = generateDropStatementsForProceduresWithoutDependents ();
152+ for (String dropStatement : dropStatements ) {
153+ jdbcTemplate .execute (dropStatement );
154+ }
155+ } while (dropStatements .size () > 0 );
156+
154157 for (Function function : allFunctions ()) {
155158 function .drop ();
156159 }
@@ -176,14 +179,16 @@ private String getSqlId() {
176179 }
177180
178181 /**
179- * Generates DROP statements for the procedures in this schema.
182+ * Generates DROP statements for the procedures in this schema that do not have dependent procedures .
180183 *
181184 * @return The drop statements.
182185 * @throws SQLException when the statements could not be generated.
183186 */
184- private List <String > generateDropStatementsForProcedures () throws SQLException {
185- String dropProcGenQuery = "select rtrim(NAME) from SYSIBM.SYSROUTINES where CAST_FUNCTION = 'N' " +
186- " and ROUTINETYPE = 'P' and SCHEMA = '" + name + "' and OWNER = '" + this .getSqlId () + "'" ;
187+ private List <String > generateDropStatementsForProceduresWithoutDependents () throws SQLException {
188+ String dropProcGenQuery = "select rtrim(sr.NAME) from SYSIBM.SYSROUTINES sr where sr.CAST_FUNCTION = 'N'" +
189+ " and sr.ROUTINETYPE = 'P' and sr.SCHEMA = '" + name + "' and sr.OWNER = '" + this .getSqlId () + "'" +
190+ " and not exists ( select * from SYSIBM.SYSDEPENDENCIES sd where sd.BNAME=sr.NAME" +
191+ " and sd.BSCHEMA=sr.SCHEMA and sd.DTYPE='O' )" ;
187192 return buildDropStatements ("DROP PROCEDURE" , dropProcGenQuery );
188193 }
189194
0 commit comments