Hello,
I'm new to db2 and I'm tring to write a stored procedure to select all the records from the history table with the latest effective date, and loads them into the active table (See below). I'm seeking help with the syntax as I keep getting errors. Thanks in advance.
CREATE PROCEDURE ED.SP_LOAD_ACTIVE_TABLE
(IN TBL_NAME VARCHAR(150), IN PRIMARY_KEY_NAME INT, IN SRC_SYS_CD VARCHAR(20), SCHEMA_NAME VARCHAR(10))
LANGUAGE SQL
DYNAMIC RESULT SETS 1
BEGIN
-- Local variable declaration
DECLARE @TBL_NAME VARCHAR(150);
DECLARE @PRIMARY_KEY_NAME INT;
DECLARE @SRC_SYS_CD VARCHAR(150);
DECLARE @SCHEMA_NAME VARCHAR(150);
INSERT INTO @SCHEMA_NAME.@TBL_NAME
SELECT HISTORY.* FROM @SCHEMA_NAME.@TBL_NAME_HST as HISTORY
WHERE @PRIMARY_KEY_NAME IN
(SELECT @PRIMARY_KEY_NAME, MAX(EFF_DT)as MAXDATE
FROM @SCHEMA_NAME.@TBL_NAME
GROUP BY @PRIMARY_KEY_NAME)as CURR
AND HIST.@PRIMARY_KEY_NAME = CURR.@PRIMARY_KEY_NAME
AND HIST.EFF_DT = CURR.MAXDATE
AND @SRC_SYS_CD IN ('AL','PS','ME','MP','SS')
END
I'm new to db2 and I'm tring to write a stored procedure to select all the records from the history table with the latest effective date, and loads them into the active table (See below). I'm seeking help with the syntax as I keep getting errors. Thanks in advance.
CREATE PROCEDURE ED.SP_LOAD_ACTIVE_TABLE
(IN TBL_NAME VARCHAR(150), IN PRIMARY_KEY_NAME INT, IN SRC_SYS_CD VARCHAR(20), SCHEMA_NAME VARCHAR(10))
LANGUAGE SQL
DYNAMIC RESULT SETS 1
BEGIN
-- Local variable declaration
DECLARE @TBL_NAME VARCHAR(150);
DECLARE @PRIMARY_KEY_NAME INT;
DECLARE @SRC_SYS_CD VARCHAR(150);
DECLARE @SCHEMA_NAME VARCHAR(150);
INSERT INTO @SCHEMA_NAME.@TBL_NAME
SELECT HISTORY.* FROM @SCHEMA_NAME.@TBL_NAME_HST as HISTORY
WHERE @PRIMARY_KEY_NAME IN
(SELECT @PRIMARY_KEY_NAME, MAX(EFF_DT)as MAXDATE
FROM @SCHEMA_NAME.@TBL_NAME
GROUP BY @PRIMARY_KEY_NAME)as CURR
AND HIST.@PRIMARY_KEY_NAME = CURR.@PRIMARY_KEY_NAME
AND HIST.EFF_DT = CURR.MAXDATE
AND @SRC_SYS_CD IN ('AL','PS','ME','MP','SS')
END