It's not necessary to write an sproc for this purpose, unless you have special requirements. You can ask Db2 to do the work via:
CALL SYSPROC.ADMIN_REVALIDATE_DB_OBJECTS('table', current schema, NULL)
You can read about that functionality here.
Otherwise , if you do have special requirements per the reorg, it may be simpler to use a cursor to find fully-qualified-table-names, i.e use the cursor to populate an array and iterate over that array to do the reorg.
Be extremely careful using offline reorgs on production environments, so you might want to build-in some defensive code to prevent causing service outages.
Otherwise you can process a result-set returned from a stored procedure using the techniques mentioned in the documentation at "Receiving procedure result sets in SQL routines"
Additionally if your Db2 database was created to support Oracle compatibilility, you can use Oracle PLSQL syntax and only add the ADMIN_CMD() stuff. You don't have to use the ANSI SQLPL style of Db2 in that case.
Here is a dumb bare bones example that uses a cursor and an array, there are many other ways to do this including more elegant ways:
--#SET TERMINATOR @
create or replace type reorg_list as VARCHAR(1024) array[]
@
create or replace procedure sp_reorgpending ()
language sql
specific sp_reorgpending
begin
declare v_sqlcode integer default 0;
declare v_stmt varchar(1024);
declare v_tabname varchar(1024);
declare tables_to_reorg reorg_list;
declare v_counter integer;
declare c1 cursor with hold for select rtrim(tabschema)||'.'||rtrim(tabname) from SYSIBMADM.ADMINTABINFO where REORG_PENDING = 'Y' AND TABSCHEMA=CURRENT SCHEMA;
declare continue handler for not found set v_sqlcode=100;
set v_counter=0;
open c1;
fetch c1 into v_tabname;
while ( v_sqlcode = 0 ) do
set v_counter = v_counter + 1 ;
set tables_to_reorg[v_counter] = v_tabname;
fetch c1 into v_tabname;
end while;
close c1;
while ( v_counter > 0 ) do
set v_stmt='reorg table '||tables_to_reorg[v_counter] ;
call admin_cmd(v_stmt);
set v_counter = v_counter -1 ;
end while;
return 0;
end
@