473,791 Members | 3,186 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 2160
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.co m> wrote in message
news:GV******** *******@news.us west.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
2611
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 the query was successful. Things to know: 1. id field is auto-incrementing primary key 2. Other queries SELECT, UPDATE, and DELETE all work no problem 3. INSERT works via phpMyAdmin 4. Query generated by phpMyAdmin and pasted into my PHP...
2
3211
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 : INSERT INTO temp_tab VALUES (1,2,3)
0
2263
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 assistance. I say additional because I've already had help which is greatly appreciated. I do try to take the time and understand the provided script in hopes on not having to trouble others on those. But here it goes...
1
6211
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. Insert and Update trigger work fine when i have only one of them defined. However when I have all the 3 triggers in place and when i try to fire a insert query on the statement. It triggers both insert and update trigger at the same time and...
2
4816
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 sql server is my backend server. This error occured whenever i step through and when it reaches rs.update it jux hangs down there and thereafter it shwn the aforementioned error. I am using the RecordSet properties to add the values. Can I use...
6
3474
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 using MS-Access 2000 database table for this app. Note that the datatype of all the fields mentioned above are Text. Apart from the above columns, there's another column in the DB table named 'RegDateTime' whose datatype is Date/Time which is...
0
2292
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 which means i hav 2 record for every insert operation. any help appreciated. thank u herez teh code i tried. ALTER TRIGGER trg_ContactAddress_Audit ON Address FOR DELETE, INSERT, UPDATE AS
1
3765
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) but it shows warning that customer can't append all the records in the append query. Customer set 1 field(s) to Null due to a type conersion failure,and it didn't add 0 record(s) to the table due to key violation, 0 record(s) due to lock...
6
9522
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 from $_POST data. All posted variables have to use the same name as the respective column in the database for this to work. I also have know what the primary - auto increment column is so I do not try to insert or update it. ...
0
9517
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10428
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9030
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
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 we have to send another system
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.