The simpler way to get a single value (or a single row) in DB2 for LUW is use of the SET
statement.
--#SET TERMINATOR @
SET SERVEROUTPUT ON@
BEGIN
DECLARE V_TABSCHEMA VARCHAR(128) DEFAULT 'SYSCAT';
DECLARE V_TABNAME VARCHAR(128);
DECLARE V_COLCOUNT INT;
PREPARE S1 FROM 'SET (?, ?) = (SELECT TABNAME, COLCOUNT FROM SYSCAT.TABLES WHERE TABSCHEMA = ? FETCH FIRST 1 ROW ONLY)';
EXECUTE S1 INTO V_TABNAME, V_COLCOUNT USING V_TABSCHEMA;
CALL DBMS_OUTPUT.PUT_LINE('Tabname: ' || V_TABNAME || ', Column count: ' || TO_CHAR(V_COLCOUNT));
END@
SET SERVEROUTPUT OFF@
Note, that it's your responsibility to make the subselect return not more than 1 row, because you get an error otherwise.
I achieve this with the FETCH FIRST
clause in the example above, since the subselect may return multiple rows without it.