Skip to content
Advertisement

This procedure is inserting null values into my custom table – why?

CREATE OR REPLACE PROCEDURE SRM_H_PROC(
    EMPLID_OUT OUT VARCHAR2,
    ACAD_CAREER_OUT OUT VARCHAR2,
    ACAD_PROG_INOUT IN OUT VARCHAR2,
    PROG_STATUS_OUT OUT VARCHAR2,
    ADMIT_TERM_INOUT IN OUT VARCHAR2)
AS
  CURSOR PROC2
  IS
    SELECT
      A.EMPLID,
      A.ACAD_CAREER,
      A.ACAD_PROG,
      A.PROG_STATUS,
      A.ADMIT_TERM
    FROM
      PS_ACAD_PROG A
    WHERE
      A.ADMIT_TERM  = ADMIT_TERM_INOUT
    AND A.ACAD_PROG =ACAD_PROG_INOUT
    AND A.EFFDT     =
      (
        SELECT MAX(EFFDT) FROM PS_ACAD_PROG WHERE EMPLID=A.EMPLID
      )
  AND A.EFFSEQ=
    (
      SELECT
        MAX(EFFSEQ)
      FROM
        PS_ACAD_PROG
      WHERE
        EMPLID =A.EMPLID
      AND EFFDT=A.EFFDT
    )
  AND A.PROG_STATUS='AC';
BEGIN
  FOR I IN PROC2
  LOOP
    IF ADMIT_TERM_INOUT='1701' THEN
      INSERT
      INTO
        PS_SRM_H_PROC_TB VALUES
        (
          EMPLID_OUT,
          ACAD_CAREER_OUT,
          ACAD_PROG_INOUT,
          PROG_STATUS_OUT,
          ADMIT_TERM_INOUT
        );
    ELSE
      DBMS_OUTPUT.PUT_LINE('HI DEAR, SAY HELLO TO ME.');
    END IF;
  END LOOP;
  COMMIT;
END SRM_H_PROC;

Table output is like this when I pass acad_prog ‘BTRCS’ and admit_term ‘1701’ as input in procedure:

(NULL)  (NULL)  BTRCS   (NULL)  1701
(NULL)  (NULL)  BTRCS   (NULL)  1701
(NULL)  (NULL)  BTRCS   (NULL)  1701
(NULL)  (NULL)  BTRCS   (NULL)  1701
(NULL)  (NULL)  BTRCS   (NULL)  1701
(NULL)  (NULL)  BTRCS   (NULL)  1701

Advertisement

Answer

You need to use cursor values in if statement and in insert statement like this:

FOR I IN PROC2
  LOOP
    IF I.ADMIT_TERM ='1701' THEN
      INSERT
      INTO
        PS_SRM_H_PROC_TB VALUES
        (
          I.EMPLID,
          I.ACAD_CAREER,
          I.ACAD_PROG,
          I.PROG_STATUS,
          I.ADMIT_TERM
        );
    ELSE
      DBMS_OUTPUT.PUT_LINE('HI DEAR, SAY HELLO TO ME.');
    END IF;
  END LOOP;

This can also be done using insert into .. select .. (without cursor) as following:

CREATE OR REPLACE PROCEDURE SRM_H_PROC(
    EMPLID_OUT OUT VARCHAR2,
    ACAD_CAREER_OUT OUT VARCHAR2,
    ACAD_PROG_INOUT IN OUT VARCHAR2,
    PROG_STATUS_OUT OUT VARCHAR2,
    ADMIT_TERM_INOUT IN OUT VARCHAR2)
AS
BEGIN 
      INSERT
      INTO
        PS_SRM_H_PROC_TB 
      SELECT
      A.EMPLID,
      A.ACAD_CAREER,
      A.ACAD_PROG,
      A.PROG_STATUS,
      A.ADMIT_TERM
    FROM
      PS_ACAD_PROG A
    WHERE
      A.ADMIT_TERM  = ADMIT_TERM_INOUT
    AND A.ACAD_PROG =ACAD_PROG_INOUT
    AND A.EFFDT     =
      (
        SELECT MAX(EFFDT) FROM PS_ACAD_PROG WHERE EMPLID=A.EMPLID
      )
  AND A.EFFSEQ=
    (
      SELECT
        MAX(EFFSEQ)
      FROM
        PS_ACAD_PROG
      WHERE
        EMPLID =A.EMPLID
      AND EFFDT=A.EFFDT
    )
     AND A.PROG_STATUS='AC'
     AND A.ADMIT_TERM ='1701';
  COMMIT;
END SRM_H_PROC;

Cheers!!

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement