I'm trying to create a dynamic SQL procedure to get the first name given an ID.
CREATE OR REPLACE PROCEDURE SELECT_12
(
MIN_NBR NUMBER
, BORR_FST_NM VARCHAR2
, FIELD_NAME VARCHAR2
) IS
TYPE cur_type IS REF CURSOR;
C_1 CUR_TYPE;
QUERY_STR VARCHAR2(1000);
FIRST_NAME VARCHAR(1000);
BEGIN
QUERY_STR:= 'SELECT BORR_FST_NM from MON_DD_DDS.' || field_name ||'
WHERE MIN_NBR = :MINNBR';
OPEN C_1 FOR QUERY_STR USING MIN_NBR;
LOOP
FETCH C_1 INTO FIRST_NAME;
EXIT WHEN C_1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(FIRST_NAME);
END LOOP;
NULL;
END SELECT_12;
I have different databases with the type MON_DD_DMS.STAGE_MRE_() where the bracket contains may different names like student, professor, scholar etc. I would like the user to define the name of the table like MON_DD_DMS.STAGE_MRE_STUDENT and MON_DD_DMS.STAGE_MRE_PROFESSOR etc.
When I try to run the procedure:
DECLARE
MIN_NBR NUMBER;
BORR_FST_NM VARCHAR2(200);
FIELD_NAME VARCHAR2(200);
BEGIN
FIELD_NAME := &FIELD_NAME;
MIN_NBR := &MIN_NBR;
BORR_FST_NM := NULL;
SELECT_12( MIN_NBR => MIN_NBR, BORR_FST_NM => BORR_FST_NM,
FIELD_NAME => FIELD_NAME );
END;
it says that STAGE_MRE_student must be declared.
So is there something wrong I'm doing? Any help would be appreciated.
DECLARE MIN_NBR NUMBER; BORR_FST_NM VARCHAR2(200); FIELD_NAME VARCHAR2(200); BEGIN FIELD_NAME := &FIELD_NAME; MIN_NBR := &MIN_NBR; BORR_FST_NM := NULL; SELECT_12( MIN_NBR => MIN_NBR, BORR_FST_NM => BORR_FST_NM, FIELD_NAME => FIELD_NAME ); END;