473,509 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert & Query Query Question

I have a two part question:

First I want to insert data into a table and I am using the following
command:

INSERT INTO tblmain
SELECT field1, field2, etc...
FROM tblimport
WHERE ??????
The where clause is the part I am having trouble with. I want to only insert
the rows where field F1 one doesn't exist in table tblmain. What would the
where clause look like? I am using MS-Access to transfer the data from a
flat file to a SQL data base because I do not know that much SQL
programming.
Second part of the question is that after the data is inserted I want to
pull out a field in tblmain (pat_id which is only generated when a new line
of data is inserted into tblmain) and place the value in the table tblimport
(in field pat_id) but only where fields F1 match in both tables.

I don't want to find the pat_id when I insert data because there will be
cases when the data in the tblimport is already in the tblmain and I don't
want to change the data in the tblmain.

Does that make any sense? It sounded ok in my head...

Thanks in advance...

Mike Charney
Mar 27 '06 #1
3 2147
I will try to help you out with part 1 for starters.

First I want to insert data into a table and I am using the following
command:

INSERT INTO tblmain
SELECT field1, field2, etc...
FROM tblimport
WHERE ??????
The where clause is the part I am having trouble with. I want to only
insert the rows where field F1 one doesn't exist in table tblmain. What
would the where clause look like? I am using MS-Access to transfer the
data from a
flat file to a SQL data base because I do not know that much SQL
programming.
<<

It sounds like you created a table and did not assign any column names,
so Access is using default column names of F1, F2, F3,... If this is
the case, then I would start out by first entering the respective column
names in your tblMain, otherwise you will be spinning you wheels for a
while trying to get the fields straight.

Next, you state that you want to insert rows into tblMain where field F1
doesn't exist. Do you mean where field F1 is null in tblMain? If this
is what you mean then it sounds like what you really want to do is to
update rows that already exist in tblMain. Else, if you mean that field
F1 in tblImport Is Null, then just specify

Insert Into tblMain select * from tblImport where F1 Is Null

HTH
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Mar 27 '06 #2

"Rich P" <rp*****@aol.com> wrote in message
news:GV***************@news.uswest.net...
I will try to help you out with part 1 for starters.

First I want to insert data into a table and I am using the following
command:

INSERT INTO tblmain
SELECT field1, field2, etc...
FROM tblimport
WHERE ??????
The where clause is the part I am having trouble with. I want to only
insert the rows where field F1 one doesn't exist in table tblmain. What
would the where clause look like? I am using MS-Access to transfer the
data from a
flat file to a SQL data base because I do not know that much SQL
programming.
<<

It sounds like you created a table and did not assign any column names,
so Access is using default column names of F1, F2, F3,... If this is
the case, then I would start out by first entering the respective column
names in your tblMain, otherwise you will be spinning you wheels for a
while trying to get the fields straight.

Next, you state that you want to insert rows into tblMain where field F1
doesn't exist. Do you mean where field F1 is null in tblMain? If this
is what you mean then it sounds like what you really want to do is to
update rows that already exist in tblMain. Else, if you mean that field
F1 in tblImport Is Null, then just specify

Insert Into tblMain select * from tblImport where F1 Is Null

HTH
Rich

*** Sent via Developersdex http://www.developersdex.com ***


Rich,

Not exactly. I am importing data to a table (tblimport) and then inserting
the rows that don't already exist in to tblmain. As for the column names, I
was reading some web pages and just came across that. I will deal with that
right now but I still don't know how to write the SQL insert statement.

Thanks for the help.
Mar 27 '06 #3
OK. It sounds like tblImport and tblMain have the same structure.
Going with this idea, I hope your tables have a key field that uniquely
identifies each row (or composite key fields). If so, you can do this:

Insert Into tblMain Select * from tblImport t1 left Join tblMain t2 On
t1.RecordID = t2.RecordID Where t2.RecordID Is Null

This query will pick up all the records from tblImport which are
currently not in tblMain - based on the key field of RecordID --> where
t1.RecordID Is Null.
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Mar 27 '06 #4

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

Similar topics

10
2584
by: johnnyboy10017 | last post by:
I am having a hell of a time with what I think is a very simple query: It won't actually insert a new record into the specified table, but returns no error, in fact it returns "1" (or true) that...
2
3169
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request :...
0
2235
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
1
6190
by: abhi81 | last post by:
Hello All, I have a table on which I have created a insert,Update and a Delete trigger. All these triggers write a entry to another audit table with the unique key for each table and the timestamp....
2
4787
by: technocraze | last post by:
Hi guys, I have encountered this error when updating the values to the MS Acess table. Error : Update on linked table failed. ODBC sql server error Timeout expired. MS Acess is my front end and...
6
3440
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am...
0
2274
by: magnolia | last post by:
i created a trigger that will record the changes made to a table .everything works fine except the insert query.whenerever i try to insert a record it fires insert and update triger at the same time...
1
3744
by: billa856 | last post by:
Hi, I am trying to insert Null value in column(ShipDate) in my table.That column(ShipDate)'s type id date/time and format is short date. I am using "" to insert Null in that column(ShipDate)...
6
9500
by: Chuck Anderson | last post by:
Granted, this is mostly a mysql question, but I think understanding the Php makes it easy to understand why my question is necessary. I'm writing a Php loop to create INSERT and UPDATE queries...
0
7234
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
7136
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
7344
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,...
1
7069
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
7505
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...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.