I am working on a DB2 SQL code.
I need to get the count of records from a list of tables. The table details will be fetched from a cursor with select.
To get the count of records I have been trying with a SELECT INTO
statement. Since the table names will be varying, I am using a dynamic SQL code.
I am sharing the piece of code that I have been trying.
I am not quite sure of the syntax while using DB2 SELECT INTO
and Dynamic SQL combination. I am getting the following error with the below attempt.
Can anyone tell me why this is so? If possible, appreciate if you could share a working code of DB2 select into and dynamic sql.
SQL0104N An unexpected token "statmnt2" was found following "SET statmnt2 := 'set ? = (SELECT COD_TIPO_ARQU FROM '||indbn". Expected tokens may include: "".
DECLARE
indbnm VARCHAR(30);
intblnm VARCHAR(30);
v_errorText varchar2(50);
statmnt2 VARCHAR(1000);
VAR_COD_TIPO_ARQU CHAR(1);
stmt1 STATEMENT;
statmnt2 VARCCHAR2(100);
BEGIN
indbnm := "db2inst5";
intblnm:= "rules";
SET statmnt2 := 'set ? = (SELECT COD_TIPO_ARQU FROM '||indbnm||'.'||intblnm||' FETCH FIRST 1 ROWS ONLY)';
PREPARE stmt1 FROM statmnt2;
EXECUTE stmt1 into VAR_COD_TIPO_ARQU ;
DBMS_OUTPUT.PUT_LINE(VAR_COD_TIPO_ARQU);
EXCEPTION
WHEN OTHERS THEN
v_errorText :=SUBSTR(SQLERRM,1, 1024);
DBMS_OUTPUT.PUT_LINE('FAILED WITH MESSAGE: '||v_errorText);
END;