need solution [message #338911] |
Wed, 06 August 2008 04:33 |
kuldip
Messages: 10 Registered: November 2007
|
Junior Member |
|
|
I have 2 tables... Table_A and Table_B
I need to update Table_A.ColumnB with the values from Table_B.ColumnB where Table_A.ColumnA = Table_B.ColumnA. Here's what I've tried so far:
update Table_A
set Table_A.ColumnB = (select Table_B.ColumnB
where Table_A.ClolumnA = Table_B.ClolumnA)
|
|
|
|
|
|
Re: need solution [message #338929 is a reply to message #338923] |
Wed, 06 August 2008 04:57 |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
I reckon that either it set lots of values to NULL (possibly raising an error), or it caused cthonian abominations to inhabit the server room (fixed in 11g)
|
|
|
Re: need solution [message #338999 is a reply to message #338929] |
Wed, 06 August 2008 07:42 |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
Or overlooking the first problem, you cannot get TOO_MANY_ROWS or NULLs with invalid syntax right off the bat (ie. FROM is missing).
|
|
|
|
Re: need solution [message #339014 is a reply to message #338911] |
Wed, 06 August 2008 08:55 |
Olivia
Messages: 519 Registered: June 2008
|
Senior Member |
|
|
SQL> update table_a
set field_a2 = (select filed_b2 from table_a where field_a1 = table_b.field_b1)
where exists (select null from table_a where field_a1 = table_b.field_b1);
Let me check...
SQL> CREATE TABLE TEST_V003 ( EMP_ID VARCHAR2(3) primary key, SAL NUMBER(8,2));
Table created.
SQL> INSERT INTO TEST_V001 VALUES ('&A',&B);
Enter value for a: 1
Enter value for b: 8000
old 1: INSERT INTO TEST_V001 VALUES ('&A',&B)
new 1: INSERT INTO TEST_V001 VALUES ('1',8000)
1 row created.
SQL> /
Enter value for a: 2
Enter value for b: 9000
old 1: INSERT INTO TEST_V001 VALUES ('&A',&B)
new 1: INSERT INTO TEST_V001 VALUES ('2',9000)
1 row created.
SQL> COMMIT;
Commit complete.
SQL> CREATE TABLE TEST_V002 ( ID VARCHAR2(3) primary key, SAL NUMBER(8,2));
Table created.
SQL> INSERT INTO TEST_V002 VALUES ('&A',&B);
Enter value for a: 1
Enter value for b: 4000
old 1: INSERT INTO TEST_V002 VALUES ('&A',&B)
new 1: INSERT INTO TEST_V002 VALUES ('1',4000)
1 row created.
SQL> /
Enter value for a: 2
Enter value for b: 9999
old 1: INSERT INTO TEST_V002 VALUES ('&A',&B)
new 1: INSERT INTO TEST_V002 VALUES ('2',9999)
1 row created.
SQL> /
Enter value for a: 3
Enter value for b: 9998
old 1: INSERT INTO TEST_V002 VALUES ('&A',&B)
new 1: INSERT INTO TEST_V002 VALUES ('3',9998)
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM TEST_V001
2 ;
EMP SAL
--- ----------
1 8000
2 9000
SQL> ED
Wrote file afiedt.buf
1* SELECT * FROM TEST_V002
2 /
ID SAL
--- ----------
1 4000
2 9999
3 9998
SQL> ed
Wrote file afiedt.buf
1 update test_v002
2 set sal = (select sal from test_v001 where emp_id = test_v002.id)
3* where exists (select null from test_v001 where emp_id = test_v002.id)
SQL> /
2 rows updated.
SQL> select * from test_v002;
ID SAL
--- ----------
1 8000
2 9000
3 9998
Regards,
Oli
[Updated on: Wed, 06 August 2008 09:06] Report message to a moderator
|
|
|
Re: need solution [message #339060 is a reply to message #339014] |
Wed, 06 August 2008 12:24 |
Frank
Messages: 7901 Registered: March 2000
|
Senior Member |
|
|
@Olivia: you have been around here at OraFAQ now for some time. You started out as an poster asking help, asking questions. Now be honest: what way did you learn more, replies that provided you ready-made solutions or replies that asked you how you would do things, asking you to think and rethink your problem?
|
|
|
Re: need solution [message #339198 is a reply to message #339060] |
Thu, 07 August 2008 01:17 |
Olivia
Messages: 519 Registered: June 2008
|
Senior Member |
|
|
Frank wrote on Wed, 06 August 2008 12:24 | @Olivia: you have been around here at OraFAQ now for some time. You started out as an poster asking help, asking questions. Now be honest: what way did you learn more, replies that provided you ready-made solutions or replies that asked you how you would do things, asking you to think and rethink your problem?
|
Sorry, I will try to avoid providing any ready-made solutions next time.
The replies that asked me how I should do the things, asking me to rethink helped me a lot rather than the ready made answers.
Regards,
Oli
[Updated on: Thu, 07 August 2008 01:17] Report message to a moderator
|
|
|