Solved

Access/SQL Comparing records against Composite Primary Key

Posted on 2016-09-19
8
59 Views
Last Modified: 2016-09-25
I have a table in SQL Server with a number of fields, i have created a composite PK of LoanNumber, BorrowerName, LenderNumber; I need to compare a new data set that contains these and other fields to determine if there are any new PK on the new data set, which will then be added to the original table.

Thank you!



Keyword Tags:
VBA, SQL, SQL Server, Access, VB
0
Comment
Question by:C Allen
8 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41804524
>>I need to compare a new data set that contains these and other fields to determine if there are any new PK on the new data set, which will then be added to the original table.

first, where you want to compare these records? in Ms SQL Server,Access, etc? reason being there are different approaches we can do this.
0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
ID: 41804617
Then you can use LEFT JOIN with the two table to get the new records.


Which DB you are referring ? SQL Server ?
0
 
LVL 1

Author Comment

by:C Allen
ID: 41804676
comparing access table (comma delimited, imported, error checked and formated) in code. has same field names but no PK as the SQL Server table (ODBC Linked).
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 41805125
Although joining the Access table to the SQL Server table is possible, it is inefficient since Access will send a request to download the ENTIRE SQL Server table and perform the join locally.  If the SQL Server table isn't large, then it won't be a problem but once you get to more than 100,000, you probably want to do something different.  Uploading the Access data to a temp table in SQL Server might prove to be more efficient if the Access table is very small and the server side table is very large.  Then you can do the left join in SQL Server.

The left join will return the "new" records.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 1

Author Comment

by:C Allen
ID: 41805309
left join on each field = corresponding field?
0
 
LVL 1

Author Comment

by:C Allen
ID: 41805321
Pat that is to say,

Loaded the access able to a temp table in SQL called tempNewLoandata

SELECT *
FROM tempNewLoanData
LEFT JOIN MasterLoanData
ON tempNewLoanData.LoanNumber = MasterLoanData.LoanNumber AND   tempNewLoanData.BorrowerName = MasterLoanData.BorrowerName AND tempNewLoanData.LenderNumber = MasterLoanData.LenderNumber

?
0
 
LVL 34

Accepted Solution

by:
PatHartman earned 500 total points
ID: 41805335
SELECT *
FROM tempNewLoanData
LEFT JOIN MasterLoanData
ON tempNewLoanData.LoanNumber = MasterLoanData.LoanNumber AND   tempNewLoanData.BorrowerName = MasterLoanData.BorrowerName AND tempNewLoanData.LenderNumber = MasterLoanData.LenderNumber

WHERE MasterLoanData.UniqueID Is Null;

You need a where clause.  If you don't have an autonumber PK, then just use one of the join fields instead.
1
 
LVL 1

Author Closing Comment

by:C Allen
ID: 41815040
Thanks for the hand as always Pat.  Worked well; My brain was not firing on all cylinders that morning.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

705 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now