Link to home
Start Free TrialLog in
Avatar of jknj72
jknj72

asked on

Trying to create an Insert statement from a few queries I have

This is all in SQL Developer....

I have a Source query below and I have this pseudo code to explain everything. Its kinda simple but I need help

 Select POLICY_KEY, POLICY_NUM, POLICY_EFF_DT, POLICY_EXP_DT, POLICY_CVG_ID, POLICY_CARR_ID, ISSUE_OFFICE_NAME, INSURED_MAIL_NAME, POLICY_LCF_IND
from TEMP_BASE_PCOV_POLICY p
ORDER BY POLICY_CARR_ID ASC, POLICY_EFF_DT ASC;

WHILE (I is not null) LOOP
    --Set some variables to run the next query
    Set l_Temp_POL_pCARRID =  POLICY_CARR_ID
    and l_Temp_POL_polNbr = POLICY_NUM
    and l_Temp_POL_covEffDate =  POLICY_EFF_DT
    and l_Temp_POL_polKey = POLICY_KEY

    --Run this query with the values from 1st query as parameters
    Select CRKEY, CARR_ID, POLNBR, COV_EFFDATE, SEQ, ACTION, ACTION_EFFDT                
    From TEMP_BASE_CANCREIN_TAB
    WHERE CARR_ID = l_Temp_POL_pCARRID AND POLNBR = TRIM(l_Temp_POL_polNbr) AND COV_EFFDATE =     l_Temp_POL_covEffDate;

     I then loop through this query and set a new variable from query 2
    WHILE LOOP(j is not null) LOOP
       
         l_inCovEFFDATE :=COV_EFFDATE;
   
           --Then check CovEffDt variable from first query and the CovEffDt  variable set from 2nd query  and do an Insert based off that                
        if l_Temp_POL_covEffDate = l_inCovEFFDATE then                        
       INSERT INTO TEMP_BASE_PCOV_CANCREIN(CR_ID, CR_KEY, CR_TYPE, CR_YR, CR_MO, CR_DA)
      Values
       (j, l_Temp_POL_polKey, l_CANCREIN_Temp(j).ACTION, GET_DATE_PART('y', l_CANCREIN_Temp(j).ACTION_EFFDT),     GET_DATE_PART('m', l_CANCREIN_Temp(j).ACTION_EFFDT), GET_DATE_PART('d', l_CANCREIN_Temp(j).ACTION_EFFDT));
COMMIT;  
      LOOP j          
         end if;            

LOOP i      

Just so you know I want to define the fields Im inserting into
I need to store an id that I use j for so don't worry about that. The next value is from the
CR_ID = j from the second loop
CR_KEY =  POLICY_KEY from the first query(which is a variable in the Insert above that I declared above
CR_TYPE = ACTION field from the 2nd query
CR_YR = ACTION_EFFDT(I parse the YEAR) from 2nd query) 4 chars
CR_MO = ACTION_EFFDT(I parse the MONTH) from 2nd query) 2 chars
CR_DA = ACTION_EFFDT(I parse the DA) from 2nd query) 2 chars

Id love to do this in 1 query with all the logic still in place. Any help would be appreciated!!

THANKS!!!
Avatar of jknj72
jknj72

ASKER

I was thinking something like this

Insert based off of this query???

Select POLICY_KEY, POLICY_NUM      , POLICY_EFF_DT , POLICY_EXP_DT , POLICY_CVG_ID , POLICY_CARR_ID
from TEMP_BASE_PCOV_POLICY p
INNER JOIN TEMP_BASE_CANCREIN_TAB c ON p.POLICY_CARR_ID = c.CARR_ID
AND p.POLICY_NUM = c.POLNBR AND p.POLICY_EFF_DT = c.COV_EFFDATE
ORDER BY POLICY_CARR_ID ASC, POLICY_EFF_DT ASC;
ASKER CERTIFIED SOLUTION
Avatar of Mark Geerlings
Mark Geerlings
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jknj72

ASKER

yeah I think Ive got it...Thanks
Avatar of jknj72

ASKER

thanks