Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parameter driven db2 stored procedure help

Status
Not open for further replies.

amoise

Programmer
Nov 4, 2008
3
US
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top