Hello everyone,
I've been trying to execute a dynamic SQL statement within a stored procedure for some time now, and this is what I've got so far:
I want to be able to call it using :
I'm also storing other data in the resulting table, and it's mainly for data verification.
I was wondering if anyone has had experience with Dynamic SQL within a stored procedure on an IBM i since I've been looking through documentation for quite some time, but I can't seem to find the answer I'm looking for.
Thanks!
I've been trying to execute a dynamic SQL statement within a stored procedure for some time now, and this is what I've got so far:
Code:
CREATE OR REPLACE PROCEDURE TSTLBEL/COMPARETBL(IN TABLE1 CHAR(10),
IN TABLE2 CHAR(10),
IN LIBL CHAR(10))
LANGUAGE SQL
MODIFIES SQL DATA
BEGIN
DECLARE REQUEST VARCHAR(200);
DECLARE NBTBLCOMP INTEGER;
SET TABLE1 = CAST(TRIM(TABLE1) AS CHAR(10));
SET TABLE2 = CAST(TRIM(TABLE2) AS CHAR(10));
SET LIBL = CAST(TRIM(LIBL) AS CHAR(10));
SET REQUEST = 'SELECT COUNT(*) FROM
(SELECT * FROM ' CONCAT LIBL CONCAT '/' CONCAT TABLE1 CONCAT '
UNION ALL
SELECT * FROM ' CONCAT LIBL CONCAT '/' CONCAT TABLE2 CONCAT ')';
--Stored values
CREATE OR REPLACE TABLE QTEMP/CMPTBL(NB_ROWS INT, AFFECTED_TABLE CHAR(10));
--Dynamic SQL Statement
EXECUTE IMMEDIATE REQUEST;
--Need to store the result into NBTBLCOMP to display here
INSERT INTO QTEMP/CMPTBL(NB_ROWS, AFFECTED_TABLE)
VALUES (NBTBLCOMP, 'Union');
END;
Code:
CALL TSTLBEL/COMPARETBL('TABLE1', 'TABLE2', 'TSTLBEL');
I was wondering if anyone has had experience with Dynamic SQL within a stored procedure on an IBM i since I've been looking through documentation for quite some time, but I can't seem to find the answer I'm looking for.
Thanks!





Comment