Home » SQL & PL/SQL » SQL & PL/SQL » How to insert data in another table
How to insert data in another table [message #617637] Wed, 02 July 2014 07:39 Go to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
here tale A and table B are two tables where data type and length
are same means table A col_1 is same table B col_1 and further likewise
table A col_2 is same as table B col_2, but Table B col_2 blank here, now I
want to insert data in table B col_2 but only those row insert
which is match to table A col_1 and table B col_1 if not match then value
should be null in table B col_2

Table A
col_1 Col_2
1 A
2 B
4 C
6 D
9 E


Table B
Col_1 Col_2
1
2
3
4
5
6
7
8
9
Re: How to insert data in another table [message #617638 is a reply to message #617637] Wed, 02 July 2014 07:41 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Use MERGE statement.

And I believe what you are looking for, is an update and not insert.

Edit : Added link to docs

[Updated on: Wed, 02 July 2014 07:44]

Report message to a moderator

Re: How to insert data in another table [message #617642 is a reply to message #617638] Wed, 02 July 2014 08:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

And I believe a simple, standard, old UPDATE is what OP needs.

And I believe we will never know what OP wants and needs and if what we posted has helped as OP never fed back in his previous topics.

Re: How to insert data in another table [message #617724 is a reply to message #617642] Thu, 03 July 2014 01:37 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Output should be like here
Table B
Col_1  Col_2       
1       A
2       B
3
4       C
5
6       D
7
8
9       E
Re: How to insert data in another table [message #617725 is a reply to message #617724] Thu, 03 July 2014 01:38 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

And what you have to do has been said.

Re: How to insert data in another table [message #617846 is a reply to message #617725] Fri, 04 July 2014 00:02 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
we can update but here no need to insert then how to solve its.
Re: How to insert data in another table [message #617852 is a reply to message #617846] Fri, 04 July 2014 01:23 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Use UPDATE.

Re: How to insert data in another table [message #617871 is a reply to message #617852] Fri, 04 July 2014 02:55 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Ok Michel I will try it.
Re: How to insert data in another table [message #617884 is a reply to message #617871] Fri, 04 July 2014 03:46 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Thank you so much for helping us. problem solved now.
Re: How to insert data in another table [message #617893 is a reply to message #617884] Fri, 04 July 2014 04:47 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
And how exactly... What code you tried?
Re: How to insert data in another table [message #617918 is a reply to message #617893] Fri, 04 July 2014 07:26 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Its Simple.

UPDATE B
SET col_2 = (SELECT col_2
FROM A
WHERE A.Col_1 = B.col_1);
Re: How to insert data in another table [message #617927 is a reply to message #617918] Fri, 04 July 2014 09:36 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Note that this update ALL rows of table A (with NULL for not matching rows but anyway it does it).
To avoid too much work restrict the updated rows:
UPDATE B
SET col_2 = (SELECT col_2 FROM A WHERE A.Col_1 = B.col_1)
where exists (select null from A where A.Col_1 = B.col_1
/

Re: How to insert data in another table [message #617952 is a reply to message #617927] Sat, 05 July 2014 02:41 Go to previous message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Thank you so much Michel.
Previous Topic: Why we not use out parameter in pl/sql function
Next Topic: Conditional Join?
Goto Forum:
  


Current Time: Thu Apr 25 23:53:32 CDT 2024