Question

I'm currently using Oracle Application Express. I am having trouble with some PL/SQL coding.

I pretty much have an application where I have applicants and appointees. Let's say I have three tables involved. One table is for applicants, one table is for appointees, and the third table is basically a table that holds information stating which boards they belong to, are they a current member of the board or not, the date started, date ended, etc.

The code I’m trying to implement is basically for when applicants are in a situation when they apply and their status is changed to "Accepted" then the applicant becomes an appointee and I insert the applicant's personal information (name, DOB, address, etc) into the appointee's table. I’m also trying to implement where when the status is changed to "Accepted" that I also am able to input (Date Start, Date End, Notes, etc.) but I want for that information to go to the third table which is the one that holds the information in which board they belong to, date started, date ended, etc. I need for both the third table and the appointees table to be linked together when this is done so that when I go to an appointee form it will show all of this information together. The third table and appointees table should have the same ID number for appointees.

If I code it like this then I can get the information to go to the appointees table from the applicant table no problem whenever the status is changed to accepted

DECLARE
BEGIN
  INSERT INTO tbl_dbo_appointees (applname, appfname, appsuf, appdob, race, sex, appadd,
                                  appstnum, appstdir, appstname, appsttype, appsufdir, appcity, appst, appzip,
                                  appphone, appwnbr, appcnbr, appemail, mappadd1, mappadd2, mcity, mstate, mzip,
                                  appedu, appothr, appwexp)
    SELECT
      bapplname, bappfname, bappnsuf, bappdob, race, sex, bappstname,
      bappstnum, bappstdir, bappstname, bappsttype, bappsufdir, bappcity, bappst, bappzip,
      bapphnbr, bappwnbr, bappcnbr, bappemail, bmappadd1, bmappadd2, bmcity, bmstate, bmzip,
      bappeducation, bappother, bappwexp     
    FROM tbl_dbo_wapplicant
    WHERE bappstatus = 'Accepted'
          AND "BAPPID" = :P55_BAPPID;
END;

I’ve tried different variations of this code to add to the third table and I’ve also tried to separate the two processes. It will write to the table but I cannot get both IDs to link together for the same appointee. declare.

begin 

INSERT INTO TBL_DBO_BRDAPPT (BRDID, BRDAPPDATE, BRDEXPDATE, BRDAPPBY,      BANOTE, BRDASSC)

SELECT BAPPBID, BAPPTBDATE, BAPPTEDATE, BAPPTBY, APPNOTE, BABRDASSC

FROM TBL_DBO_WAPPLICANT

WHERE BAPPSTATUS = 'Accepted'

and

"BAPPID" = :P55_BAPPID;

END;

The common ID for Appointees is labeled APPID in the Appointees table and BRDAPT in the third table.

CREATE TABLE BRDAPPT
(
  BAID        NUMBER,
  BRDID       NUMBER,
  BRDAPT      NUMBER,
  BRDAPPDATE  DATE,
  BRDEXPDATE  DATE,
  BRDAPPBY    NUMBER,
  BACTIVE     VARCHAR2(30 BYTE),
  BANOTE      VARCHAR2(255 BYTE),
  BRDONBRD    NUMBER,
  BRDYRS      NUMBER,
  BRDASSC     VARCHAR2(255 BYTE),
  BRDRAPPT    VARCHAR2(30 BYTE)



 CREATE TABLE WAPPLICANT
(
  BAPPID         NUMBER,
  BAPPBID        NUMBER,
  BAPPLNAME      VARCHAR2(30 BYTE),
  BAPPFNAME      VARCHAR2(30 BYTE),
  BAPPMINIT      VARCHAR2(1 BYTE),
  BAPPNSUF       VARCHAR2(30 BYTE),
  BAPPSTNUM      NUMBER,
  BAPPSTDIR      VARCHAR2(1 BYTE),
  BAPPSTNAME     VARCHAR2(30 BYTE),
  BAPPSTTYPE     VARCHAR2(30 BYTE),
  BAPPSUFDIR     VARCHAR2(1 BYTE),
  BAPPCITY       VARCHAR2(30 BYTE),
  BAPPST         VARCHAR2(30 BYTE),
  BAPPZIP        VARCHAR2(30 BYTE),
  BAPPHNBR       VARCHAR2(10 BYTE),
  BAPPWNBR       VARCHAR2(10 BYTE),
  BAPPCNBR       VARCHAR2(10 BYTE),
  BAPPEMAIL      VARCHAR2(255 BYTE),
  BAPPEDUCATION  VARCHAR2(4000 BYTE),
  BAPPWEXP       VARCHAR2(4000 BYTE),
  BAPPOTHER      VARCHAR2(4000 BYTE),
  BAPPDOB        DATE,
  RACE           VARCHAR2(1 BYTE),
  SEX            VARCHAR2(6 BYTE),
  BAPPINDATE     DATE,
  BAPPINTYPE     VARCHAR2(30 BYTE),
  BAPPSTATUS     VARCHAR2(30 BYTE),
  BSTATDATE      DATE,
  BSTATREAS      VARCHAR2(30 BYTE),
  BAPPTBDATE     DATE,
  BAPPTEDATE     DATE,
  BAPPTBY        VARCHAR2(1 BYTE),
  BAPPSTENBR     VARCHAR2(30 BYTE),
  BMAPPADD1      VARCHAR2(30 BYTE),
  BMAPPADD2      VARCHAR2(30 BYTE),
  BMCITY         VARCHAR2(30 BYTE),
  BMSTATE        VARCHAR2(30 BYTE),
  BMZIP          VARCHAR2(30 BYTE),
  BPPSTNOTR      VARCHAR2(30 BYTE),
  IPADDRESS      VARCHAR2(30 BYTE),
  SM_TIMESTAMP   VARCHAR2(1 BYTE),
  CONVICTION     VARCHAR2(1 BYTE),
  BABRDASSC      VARCHAR2(255 BYTE),
  APPNOTE        VARCHAR2(255 BYTE)
  )


CREATE TABLE APPOINTEES
(
  APPID       NUMBER,
  APPFNAME    VARCHAR2(30 BYTE),
  APPLNAME    VARCHAR2(30 BYTE),
  APPSUF      VARCHAR2(40 BYTE),
  APPDOB      DATE,
  RACE        VARCHAR2(1 BYTE),
  SEX         VARCHAR2(1 BYTE),
  APPADD      VARCHAR2(255 BYTE),
  APPSTNUM    VARCHAR2(30 BYTE),
  APPSTDIR    VARCHAR2(1 BYTE),
  APPSTNAME   VARCHAR2(255 BYTE),
  APPSTTYPE   VARCHAR2(30 BYTE),
  APPSUFDIR   VARCHAR2(1 BYTE),
  APPCITY     VARCHAR2(40 BYTE),
  APPST       VARCHAR2(40 BYTE),
  APPZIP      VARCHAR2(40 BYTE),
  APPPHONE    VARCHAR2(40 BYTE),
  APPWNBR     VARCHAR2(40 BYTE),
  APPCNBR     VARCHAR2(40 BYTE),
  APPEMAIL    VARCHAR2(40 BYTE),
  APPSPNOTES  VARCHAR2(255 BYTE),
  ACTIVE      VARCHAR2(30 BYTE),
  MAPPADD1    VARCHAR2(40 BYTE),
  MAPPADD2    VARCHAR2(50 BYTE),
  MCITY       VARCHAR2(30 BYTE),
  MSTATE      VARCHAR2(30 BYTE),
  MZIP        VARCHAR2(30 BYTE),
  APPSTEAPT   VARCHAR2(30 BYTE),
  APPSTNOTR   VARCHAR2(30 BYTE),
  APPEDU      VARCHAR2(2000 BYTE),
  APPOTHR     VARCHAR2(4000 BYTE),
  APPWEXP     VARCHAR2(4000 BYTE),
  CONVICTQUE  VARCHAR2(1 BYTE)
)

Any suggestions on what to do? Should I combine the codes, seperate, am I missing something?

Was it helpful?

Solution

Using a trigger would be a good way of managing this task. I would place an INSERT or UPDATE conditional trigger on the WAPPLICANT table.

Setting Up Constraints and Auto-Sequenced ID/Key Columns

The values BAID, BAPPID and APPID can be managed through 3 individual Primary Key constraints. If you designed these tables through APEX, you can let Oracle manage the assignment of new index/key values:

  1. In the create table wizard, when prompted for PRIMARY KEY, Choose "Populated from a New Sequence".
  2. Select the column value you have designated as the Index/Key for the specific table you're creating.
  3. You may designate a FOREIGN KEY constraint since there is a connection between the different columns in your tables. It's good practice to make them because it prevents the possibility of orphaned records, like an appointee or a board appointment without an matching application.

If you choose to create a Foreign Key, the order that you create the tables matters... a REFERENCED Primary Key has to exist before a Foreign Key can be created to point to it. If you miss that detail, you can always alter the tables or modify them to add the FK's at a later time.

Setting Up The Trigger

Since The two additional tables are not initially populated by user input, these both can be initialized with their own block of PL/SQL code... in this example, I just stuff it into the trigger... though I advise future caution with putting too much code or logic into triggers... they can kill performance and even lock up other DML operations against the table if the trigger takes to long to finish up.

We will use the trigger to capture the BAPPID value, which you indicate is needed in the other two tables. These are the settings you should designate when creating your trigger through the Apex interface:

  1. Build the trigger on table WAPPLICANT
  2. Firing Point: AFTER
  3. Options: INSERT, UPDATE (I assumed it is possible to insert an application in Accepted status.)
  4. When: NEW.BAPPSTATUS = 'Accepted'
  5. "For Each Row" (selected)
  6. Trigger Body: (see code snippet below. The Apex "Create Trigger" dialogue will fill in most of the customary syntax...)

    INSERT INTO APPOINTEES (
       APPID, 
       APPFNAME,
       APPLNAME,
       APPSUF,
       APPDOB...
    )
    
    VALUES ( :NEW.BAPPID, :NEW.BAPPFNAME, :NEW.BAPPLNAME, :NEW.APPSUF, :NEW.APPDOB ...);
    
    INSERT INTO BRDAPPT (
       BRDID,
       BRDAPT,
       BRDAPPDATE...
     )
    
    VALUES ( :NEW.BAPPBID, :NEW.BAPPID, :NEW.BAPPTBDATE... );
    

A "COMMIT" statement is not necessary within the PL/SQL block of your trigger body. Remember, any Primary Key values that you have associated with a sequence does not need to be included within any of the INSERT commands within your PL/SQL as Apex has actually set up an auto-sequencing trigger for you.

That's it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top