| Author |
Topic |
|
smorty44
Yak Posting Veteran
93 Posts |
Posted - 2007-10-31 : 12:05:06
|
| I need to insert data from one table into another table that have different column names and I only need to insert certain fields.Should I use Insert into? And how do I reference the from table when the column names are different?Insert into Clients (Clientname, address, phone)from contacts (contactname, contactaddress, contactphone)where contactname = 'DOS' |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-10-31 : 12:07:00
|
[code]Insert into Clients (Clientname, address, phone)select contactname, contactaddress, contactphonefrom contacts where contactname = 'DOS'[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
smorty44
Yak Posting Veteran
93 Posts |
Posted - 2007-10-31 : 12:13:11
|
| What do I do for fields in my insert table that are not in my from table? Do I list these in the insert as null and ignore them in the select? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-10-31 : 12:15:47
|
You should be asking the question to yourself. What value do you want to insert it with ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
smorty44
Yak Posting Veteran
93 Posts |
Posted - 2007-10-31 : 12:22:32
|
| Sorry, I'm having trouble understanding how the insert knows which fields to grab from the select and where to put them in the table. if I set nulls in the insert then the select statement will skip this insert and select it's next field?Insert into clients (clientname, address, null, phone)select contactname, contactaddress, contactphonefrom contactswhere contacts = 'dos' |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-10-31 : 12:27:48
|
assuming your clients table has 4 columns and if you only specify 3 columns in the insert statement, the column not specified will be inserted with NULL value.Insert into clients (clientname, address, phone)select contactname, contactaddress, contactphonefrom contactswhere contacts = 'dos' If you want to insert a constant value into the column, you can do thisInsert into clients (clientname, address, phone, fax)select contactname, contactaddress, contactphone, 'NO FAX'from contactswhere contacts = 'dos' KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
smorty44
Yak Posting Veteran
93 Posts |
Posted - 2007-10-31 : 12:35:42
|
| Thank you so much KH! You're information has been extremely helpful! |
 |
|
|
arorarahul.0688
Posting Yak Master
125 Posts |
Posted - 2007-11-01 : 06:25:27
|
| try thisinsert into target_tableselect [column_name] from [source_table] where [apply conditio]Rahul Arora MCA 07 BatchNCCE Israna, PanipatHRY, INDIA |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-11-01 : 07:13:25
|
| Sorry Rahul, but I'm not understanding how that is different to khtan's suggestion. Can you explain please?Kristen |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-11-01 : 07:15:05
|
quote: Originally posted by Kristen Sorry Rahul, but I'm not understanding how that is different to khtan's suggestion. Can you explain please?Kristen
He starts posting without reading any replies. Read all his recent postsMadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-11-01 : 07:45:37
|
| What a waste of time then ... |
 |
|
|
KenW
Constraint Violating Yak Guru
391 Posts |
Posted - 2007-11-01 : 13:13:12
|
quote: Originally posted by Kristen What a waste of time then ...
I posted yesterday that at least he's consistent. 100% of his answers have been wrong for the questions asked. |
 |
|
|
Van
Constraint Violating Yak Guru
462 Posts |
Posted - 2007-11-01 : 17:10:01
|
| I noticed that too. He didn't include a column list with his INSERT section but he says to SELECT [colname]. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-11-01 : 22:02:29
|
Maybe he knew about the contest ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|