473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SQL INSERT INTO a table using information from two other tables

Max
Hi,
I have the following problem. I want to insert records into one table
using the combination of two other tables. Here is a simplified
example: Let's say the first table has a column with RaceCode (which
might have values such as 1,3,5,7, etc), the second table is like a
key for this RaceCode so it has two columns (1st one is 1,3,5,7, etc
and 2nd one with description such as Caucasian, Hispanic, African
American, Native American, etc). Now I want to insert records into the
third table which would look like the first table only instead of
values it will have descriptions but the catch is if the value doesn't
exist in the second table I need to put 'UNKNOWN' How do I accomplish
this? Here is what I have:
Table 1 Table 2 Table 3
-------------------------------------------
CODE CODE DESCRIP DESCRIP
1 1 Cauca Cauca
1 2 Hispa Cauca
2 3 Afri Amer Hispa
7 4 Nati Amer UNKNOWN
2 Hispa
1 Cauca

query = "INSERT INTO table3 (DESCRIP)
SELECT table2.DESCRIP
FROM table1, table2
WHERE table1.CODE = table2.CODE"

The problem is it will not insert the row where the CODE is 7 in
table1 but I want it to be inserted with the value of UNKNOWN. I was
trying to work with IIf function but so far was not successful. Any
help is appreciated!
Thanks.
Nov 12 '05 #1
2 2989
ma*****@hotmail.com (Max) wrote in message news:<2c**************************@posting.google. com>...
Hi,
I have the following problem. I want to insert records into one table
using the combination of two other tables. Here is a simplified
example: Let's say the first table has a column with RaceCode (which
might have values such as 1,3,5,7, etc), the second table is like a
key for this RaceCode so it has two columns (1st one is 1,3,5,7, etc
and 2nd one with description such as Caucasian, Hispanic, African
American, Native American, etc). Now I want to insert records into the
third table which would look like the first table only instead of
values it will have descriptions but the catch is if the value doesn't
exist in the second table I need to put 'UNKNOWN' How do I accomplish
this? Here is what I have:
Table 1 Table 2 Table 3
-------------------------------------------
CODE CODE DESCRIP DESCRIP
1 1 Cauca Cauca
1 2 Hispa Cauca
2 3 Afri Amer Hispa
7 4 Nati Amer UNKNOWN
2 Hispa
1 Cauca

query = "INSERT INTO table3 (DESCRIP)
SELECT table2.DESCRIP
FROM table1, table2
WHERE table1.CODE = table2.CODE"

The problem is it will not insert the row where the CODE is 7 in
table1 but I want it to be inserted with the value of UNKNOWN. I was
trying to work with IIf function but so far was not successful. Any
help is appreciated!
Thanks.


You were on the right path. Try:

INSERT INTO table3 ( DESCRIP )
SELECT IIf(IsNull(table2.DESCRIP),"UNKNOWN",table2.DESCRI P) AS MyDescrip
FROM table1 LEFT JOIN table2 ON table1.CODE = table2.CODE

Bruce
Nov 12 '05 #2
Thanks a lot it's just what i needed - works great!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ferindo Middleton Jr | last post by:
I am trying to write a Perl Function for one of the databases I'm building a web application for. This function is triggered to occur BEFORE INSERT OR UPDATE. This function is complex in that it...
1
by: Jay | last post by:
Hi I have a huge table with over 100million records and on regular basis ineed to delete nearly a million records and insert a million records. Currently I delete indexes before going through the...
0
by: rodneybauer1 | last post by:
hello: thanks in advance for any responses. i dont have a isp at home so have to use public library computer. will check this every couple days though. im using visual basic.Net windows XP with...
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
14
by: Andre | last post by:
Hello Can anyone help me translate this from access so that it can work in mssql (i need to get next value, but cannot use identity as if row is deleted, another must get new next column number...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
5
by: dotyet | last post by:
I have been given the daunting task of sql query tuning. I am looking for ways to get started with that. I am on DB2 UDB 8.2 (8.1 with Fixpak 8) on Windows. One point which I could think about...
8
by: Carl | last post by:
Hi, I hope someone can share some of their professional advice and help me out with my embarissing problem concerning an Access INSERT query. I have never attempted to create a table with...
7
by: Kevin Lawrence | last post by:
Hi all I want to do "INSERT INTO Table (Blob) Values('blobdataasstring')". ...rather than using the parameter driven method, is it possible? And if so what encoder do I use to convert the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.