Link to home
Start Free TrialLog in
Avatar of Allen Pitts
Allen PittsFlag for United States of America

asked on

PL/SQL Using Cursor Loop to insert values

Hello expert,

In previous question titled 'DECLARE CURSOR IS SELECT BEGIN'
The Cursor/Loop form was used to display a list of names and Contact IDs

Informative but not too useful.

In this question, more complex, there are attached texts to create three
tables
TMP_SOURCE_180615 has fields Contact_ID, FirstName and LastName

TMP_SOURCE_180617 has fields Contact_ID, Contact_Phone, FirstName and LastName

TMP_SOURCE_180618 has fields Contact_ID, LastName, and Contact_Phone

PS File names too long for display. Have truncated the names to the last six digits..

Ususally a script is attempted before asking a question because I don't
believe in just asking a completely open ended question like 'How do I do this?'

But have tried a couple of things and it is obvious that I am not even close.

However within the context of the script marked 'Declare Cursor TMP_SOURCE_180615'
copied herewith below what is the approach and tactic:

Where the Contact_ID is a certain value in TMP_SOURCE_180615
Fetch the CONTACT_PHONE that has that CONTACT_ID value from TMP_SOURCE_180617
and insert the CONTACT_PHONE value that matches the Contact_ID in TMP_SOURCE_180618
in the matching CONTACT_Phone field.

Again would like to make question more specific but am in uncharted waters and cannot
find a tutorial that addresses this issue.

Will double point on answer.

Allen in Dallas

---- Declare Cursor TMP_SOURCE_180615 ------
SET SERVEROUTPUT ON
DECLARE
      CURSOR cur_name
      IS
            SELECT
                  CONTACT_ID, FIRST_NAME, LAST_NAME
            FROM
                  TMP_SOURCE_180615
            ORDER BY
                  CONTACT_ID;
BEGIN
      FOR TMP_SOURCE_180615 IN cur_name
      LOOP
            DBMS_OUTPUT.PUT_LINE(TMP_SOURCE_180615.CONTACT_ID ||': '|| TMP_SOURCE_180615.FIRST_NAME || ' ' || TMP_SOURCE_180615.LAST_NAME);
      END LOOP;
END
180615.txt
180617.txt
180618.txt
180615.txt
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Script for TMP_SOURCE_180615 is missing.

Also confirm this is just for education purposes and you aren't planning on doing this in production.
ASKER CERTIFIED SOLUTION
Avatar of awking00
awking00
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
SOLUTION
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 Allen Pitts

ASKER

Thanks for the excellent input.
Slight: The script in the question was to learn the Cursor/Loop form. But it will be developed and tested in a dev environment to move data in a prod database.
Sujith: A MERGE is an elegant solution. Have a lot of scripts that use the Cursor/Loop form so need to figure what the guy was doing there.
awking: This is exactly what I needed.  Could not figure out how to join 180615 on 180617 and then write to 180618. Well done.

Also the Solution box says 'add more' so thought it meant more points. When I went to double the points it would not allow it. I feel I have mislead you and apologize.
Sujith, That's precisely what I was thinking when I made the comment that it's not the way I would go about. However, I believe the asker is trying to educate himself on how cursors are used, which is why I responded the way I did.
>> But it will be developed and tested in a dev environment to move data in a prod database.

I would STRONGLY suggest you only learn how it was done.  Then rewrite it to do it right!!!!

No need to learn the wrong way to do something and continue doing it.

You once learned to ride a bike but I bet you drive a car to work.  You learned a better way to get around that is more efficient.

Continuing to write bad code doesn't help you or your employer.