Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access Insert Query

Posted on 2011-09-20
8
Medium Priority
?
421 Views
Last Modified: 2013-11-27
I am trying to insert additional records into a table of my database, but only if they are actually new records. The records are individuals with the following fields: FirstName, LastName, Address, City, State, and Category

The records I want to insert are in an Excel 2007 spreadsheet (source). I am thinking I want to check if the Category matches, and if it does then check the destination for the last name (within that category). If the last name exists check for the first name (within that category with matching last names). If the first and last names match for a given category, skip that record in the source. If there is not a matching record in the destination, then add that source record to the destination table.

There will be way more matches that do not need inserted than the other way around. My source table may have up to 5000 records or so, and the destination table has around 50,000. Microsoft Access 2007, on Windows 7, with a MySQL table being used via ODBC connection. VBA solutions are acceptable as well.

This is going to have to be performed dozens of times with various source tables, each being a different Category.

Thanks in advance
0
Comment
Question by:jlarger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 7

Expert Comment

by:BusyMama
ID: 36569306
Can you make the SQL table have a composite primary key which includes Category, First Name and Last Name?  That would (I believe) automatically prevent any duplicates based on those fields from being loaded.

If that isn't possible, then it seems to me that Category should be your LAST check, because assuming the people on the Excel sheet can have the same category, Last Name would be a larger filter.
0
 
LVL 7

Expert Comment

by:BusyMama
ID: 36569323
Oops ... clicked Submit too soon.  Also, I would link the Access database directly to the spreadsheet as a linked table and avoid the VBA to send the Excel data to Access.
0
 

Author Comment

by:jlarger
ID: 36569361
I have not heard of composite keys or composite primary keys for that matter, I will have to look into it.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 7

Accepted Solution

by:
BusyMama earned 2000 total points
ID: 36569605
Here is an Access database with a query that you could use to find the mismatches based on the criteria you provided, and update the SQL server (even without a composite primary key).

This basically looks at the Excel spreadsheet (link your spreadsheet to Excel), and looks at the SQL table (link your SQL table to Access), makes the comparison using a left outer join on the 3 fields you indicated and appends the records which are in Excel but don't have a match in SQL to SQL.
Database5.accdb
0
 

Author Comment

by:jlarger
ID: 36570176
Your description of the solution seems spot on what I am looking for. Looking at the code though I do not understand it. Also there was an issue where category was listed twice. I deleted the duplicate and was left with the remaining code:
INSERT INTO MockSQLTable ( FirstName, LastName, Address, City, State, Category )
SELECT MockLinkedExcelSheet.FirstName, MockLinkedExcelSheet.LastName, MockLinkedExcelSheet.Address, MockLinkedExcelSheet.City, MockLinkedExcelSheet.State, MockLinkedExcelSheet.Category
FROM MockLinkedExcelSheet LEFT JOIN MockSQLTable ON (MockLinkedExcelSheet.FirstName=MockSQLTable.FirstName) AND (MockLinkedExcelSheet.LastName=MockSQLTable.LastName) AND (MockLinkedExcelSheet.Category=MockSQLTable.Category)
WHERE (((MockSQLTable.Category) Is Null));

Open in new window

Could you possibly step me through the logic of this?
0
 
LVL 7

Expert Comment

by:BusyMama
ID: 36570797
Sure ...

INSERT INTO MockSQLTable - tells the system which table you are going to place the data
(FirstName, LastName, Address, City, State, Category) - tells the system which data fields in the table you are putting data into

Then when we are telling the system WHAT data to put into the MockSQLtable, we are "selecting" it from another existing table:

SELECT MockLinkedExcelSheet.FirstName, etc.  -- the number of fields here matches the fields we placed in the INSERT INTO statement, and in the same order; the reason the table name is in front of each field is because the fields in the MockSQLTable have the same name, so this differentiates which "FirstName", etc.

FROM MockLinkedExcelSheet (tells the system which data table to SELECT the data from)

LEFT JOIN -- this is the part of the statement that matches the two tables; even though we are only going to put into the MockSQLTable what isn't already there, we have to use some kind of statement to match.  A LEFT JOIN will always return data from one table, and only matching data from another table.  In our case, we are returning all the data from the MockLinkedExcelSheet and only the matching data (see the next statement for the match requirements) from MockSQLTable.

ON -- identifies ALL the fields that must match - in your case 3 fields must match so we have AND in between each one (all three must match)

WHERE -- this serves as a filter for the data and actually goes with the INSERT statement at the start of the code -- we want to insert the data where the category in MockSQLTable is null (doesn't exist).  This might seem confusing, because your requirement is that all 3 fields (First & Last Name, and Category).  Remember we did a LEFT JOIN?  That way we had all of the data from the MockLinkedExcelSheet and matching data from the MockSQLTable - the WHERE says, OK, look at all that data, and anywhere that the matching data doesn't have a "Category" then we need to insert only those records.  We could have used first name or last name as well, or all three, but only one was necessary.

I hope that helps, I know it's a bit wordy, but easier to explain in person!
0
 

Author Comment

by:jlarger
ID: 36571073
Absolutely phenomenal work. Thank you so much, that explanation was just great. Wish I could give you more points.

I am not familiar with join queries at all which is where my confusion was, primarily with that WHERE statement and the Category being Null. Makes perfect sense now.

Thanks again!
0
 

Author Closing Comment

by:jlarger
ID: 36571076
Great help
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question