Home » SQL & PL/SQL » SQL & PL/SQL » Compare 2 tables and upadte 1 (pl sql)
icon5.gif  Compare 2 tables and upadte 1 [message #651361] Mon, 16 May 2016 09:21 Go to next message
Helen_86
Messages: 5
Registered: May 2016
Junior Member
Hello, I am newbie with pl sql.

I want to do this:

TABLE A

ID-Name-Date
1-Ralf-20160425
2-Marge-
3-Homer-20150115
4- - 20140212

TABLE B
ID-Name-Date
2-Marge-20160505
3-Homer-20150115
4-Bart - 20140212
55-Maggie-20160715

TABLE OUTPUT
1-Ralf-20160425
2-Marge-20160505
3-Homer-20150115
4-Bart- 20140212
55-Maggie-20160715

I need to compare Table A with Table B and update Table A with new registers and differente fields of table B. I wnat to wok with cursors.

How can i do that? thank you!! Razz

[Updated on: Mon, 16 May 2016 09:23]

Report message to a moderator

Re: Compare 2 tables and upadte 1 [message #651362 is a reply to message #651361] Mon, 16 May 2016 09:26 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum.

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.


What should expected desired result actually be?
Re: Compare 2 tables and upadte 1 [message #651363 is a reply to message #651362] Mon, 16 May 2016 09:32 Go to previous messageGo to next message
Helen_86
Messages: 5
Registered: May 2016
Junior Member
Hi, I need a cursor that through the records of Table A and Table B. Table A must update the data in Table B. I think it´s easy but I don´t know!! Surprised

[Updated on: Mon, 16 May 2016 09:33]

Report message to a moderator

Re: Compare 2 tables and upadte 1 [message #651364 is a reply to message #651363] Mon, 16 May 2016 09:39 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
How will you & I know when correct answer has been posted here?
Re: Compare 2 tables and upadte 1 [message #651365 is a reply to message #651361] Mon, 16 May 2016 09:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68634
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

If you post a working Test case: create table and insert statements along with the result you want with these data then we will work with your table and data.
Don't forget to read and apply How to use [code] tags and make your code easier to read.

Quote:
Table A must update the data in Table B.


It seems to me that in your example table B is up to date.

Re: Compare 2 tables and upadte 1 [message #651366 is a reply to message #651365] Mon, 16 May 2016 09:55 Go to previous messageGo to next message
cookiemonster
Messages: 13919
Registered: September 2008
Location: Rainy Manchester
Senior Member
You can do this with a simple merge statement, no PL/SQL required.
Re: Compare 2 tables and upadte 1 [message #651403 is a reply to message #651361] Tue, 17 May 2016 08:04 Go to previous messageGo to next message
javed.khan
Messages: 340
Registered: November 2006
Location: Banglore
Senior Member

Since you have not provided any working test cases or statements , let me guess there is two tables EMP and DEPT and i want to update one by overlooking on the values to another table.

CREATE TABLE EMP (EMPNO NUMBER(4) NOT NULL,
                      ENAME VARCHAR2(10),
                      JOB VARCHAR2(9),
                      MGR NUMBER(4),
                      HIREDATE DATE,
                      SAL NUMBER(7, 2),
                      COMM NUMBER(7, 2),
                      DEPTNO NUMBER(2));


INSERT INTO EMP VALUES (7369, 'SMITH', 'CLERK',    7902, TO_DATE('17-DEC-1980', 'DD-MON-YYYY'), 800, NULL, 20);
INSERT INTO EMP VALUES (7499, 'ALLEN', 'SALESMAN', 7698, TO_DATE('20-FEB-1981', 'DD-MON-YYYY'), 1600, 300, 30);
INSERT INTO EMP VALUES (7521, 'WARD',  'SALESMAN', 7698, TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250, 500, 30);
INSERT INTO EMP VALUES (7566, 'JONES', 'MANAGER',  7839, TO_DATE('2-APR-1981',  'DD-MON-YYYY'), 2975, NULL, 20);
INSERT INTO EMP VALUES (7654, 'MARTIN', 'SALESMAN', 7698,TO_DATE('28-SEP-1981', 'DD-MON-YYYY'), 1250, 1400, 30);
INSERT INTO EMP VALUES (7698, 'BLAKE', 'MANAGER', 7839,TO_DATE('1-MAY-1981', 'DD-MON-YYYY'), 2850, NULL, 30);
INSERT INTO EMP VALUES (7782, 'CLARK', 'MANAGER', 7839,TO_DATE('9-JUN-1981', 'DD-MON-YYYY'), 2450, NULL, 10);
INSERT INTO EMP VALUES (7788, 'SCOTT', 'ANALYST', 7566,TO_DATE('09-DEC-1982', 'DD-MON-YYYY'), 3000, NULL, 20);
INSERT INTO EMP VALUES (7839, 'KING', 'PRESIDENT', NULL,TO_DATE('17-NOV-1981', 'DD-MON-YYYY'), 5000, NULL, 10);
INSERT INTO EMP VALUES (7844, 'TURNER', 'SALESMAN', 7698,TO_DATE('8-SEP-1981', 'DD-MON-YYYY'), 1500, 0, 30);
INSERT INTO EMP VALUES (7876, 'ADAMS', 'CLERK', 7788,TO_DATE('12-JAN-1983', 'DD-MON-YYYY'), 1100, NULL, 20);
INSERT INTO EMP VALUES (7900, 'JAMES', 'CLERK', 7698,TO_DATE('3-DEC-1981', 'DD-MON-YYYY'), 950, NULL, 30);
INSERT INTO EMP VALUES (7902, 'FORD', 'ANALYST', 7566,TO_DATE('3-DEC-1981', 'DD-MON-YYYY'), 3000, NULL, 20);
INSERT INTO EMP VALUES (7934, 'MILLER', 'CLERK', 7782,TO_DATE('23-JAN-1982', 'DD-MON-YYYY'), 1300, NULL, 10);


CREATE TABLE DEPT (DEPTNO NUMBER(2),DNAME VARCHAR2(14),LOC VARCHAR2(13) );


INSERT INTO DEPT VALUES (10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO DEPT VALUES (20, 'RESEARCH', 'DALLAS');
INSERT INTO DEPT VALUES (30, 'SALES', 'CHICAGO');
INSERT INTO DEPT VALUES (40, 'OPERATIONS', 'BOSTON');


update records in one table based on values in another table.
UPDATE emp
       SET ename = ( SELECT dname
                      FROM dept
                      WHERE emp.deptno = dept.deptno)
        WHERE EXISTS
          ( SELECT dname
                      FROM dept
                      WHERE emp.deptno = dept.deptno);


I don't know if this is what you wanted. Post back with the details seniors asked anyway.
Re: Compare 2 tables and upadte 1 [message #651405 is a reply to message #651403] Tue, 17 May 2016 08:17 Go to previous messageGo to next message
cookiemonster
Messages: 13919
Registered: September 2008
Location: Rainy Manchester
Senior Member
It's fairly clear from the original post that missing records need adding (see entry 55), so a merge is needed, not an update.
icon14.gif  Re: Compare 2 tables and upadte 1 [message #651409 is a reply to message #651403] Tue, 17 May 2016 09:17 Go to previous messageGo to next message
Helen_86
Messages: 5
Registered: May 2016
Junior Member
Thank you. I could do with your help.
Re: Compare 2 tables and upadte 1 [message #651410 is a reply to message #651405] Tue, 17 May 2016 09:18 Go to previous messageGo to next message
Helen_86
Messages: 5
Registered: May 2016
Junior Member
cookiemonster wrote on Tue, 17 May 2016 08:17
It's fairly clear from the original post that missing records need adding (see entry 55), so a merge is needed, not an update.

How would you use this function(merge) ?
Re: Compare 2 tables and upadte 1 [message #651414 is a reply to message #651410] Tue, 17 May 2016 09:50 Go to previous messageGo to next message
Michel Cadot
Messages: 68634
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Michel Cadot wrote on Mon, 16 May 2016 16:43

If you post a working Test case: create table and insert statements along with the result you want with these data then we will work with your table and data.
Don't forget to read and apply How to use [code] tags and make your code easier to read.

Quote:
Table A must update the data in Table B.


It seems to me that in your example table B is up to date.


And post your Oracle version.


Re: Compare 2 tables and upadte 1 [message #651419 is a reply to message #651414] Tue, 17 May 2016 10:09 Go to previous messageGo to next message
Helen_86
Messages: 5
Registered: May 2016
Junior Member
SOLUTION:
MERGE INTO test1 a
USING all_objects b
ON (a.object_id = b.object_id)
WHEN MATCHED THEN
UPDATE SET a.status = b.status
WHERE b.status != 'VALID'
WHEN NOT MATCHED THEN
INSERT (object_id, status)
VALUES (b.object_id, b.status)
WHERE b.status != 'VALID';


Razz

[Updated on: Tue, 17 May 2016 10:09]

Report message to a moderator

Re: Compare 2 tables and upadte 1 [message #651421 is a reply to message #651419] Tue, 17 May 2016 10:12 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
merge INTO test1 a 
USING all_objects b 
ON (a.object_id = b.object_id) 
WHEN matched THEN 
  UPDATE SET a.status = b.status WHERE b.status != 'VALID' 
WHEN NOT matched THEN 
  INSERT (object_id, 
          status) 
  VALUES (b.object_id, 
          b.status) WHERE b.status != 'VALID';


Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Re: Compare 2 tables and upadte 1 [message #651426 is a reply to message #651419] Tue, 17 May 2016 12:10 Go to previous message
Michel Cadot
Messages: 68634
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Helen_86 wrote on Tue, 17 May 2016 17:09
SOLUTION:


How it this a solution to the problem you posted?


Previous Topic: oracle server CPU spikes
Next Topic: Not a group by expression (but not in sqlplus)
Goto Forum:
  


Current Time: Tue Apr 16 02:52:09 CDT 2024