Access - Merge two databases with identical structure - sql

I would like to write a query that merges two Access 2000 databases into one. Each has 35 tables with identical fields and mostly unique data. There are some rows which will have the same "primary key" in which case the row from database A should always take precedence over database B. I use quotes around "primary key" because the databases are generated without any keys or relationships. For example:
Database A, table1
col1 col2
Frank red
Debbie blue
Database B, table1
col1 col2
Harry orange
Debbie pink
And the results I would like:
col1 col2
Frank red
Harry orange
Debbie blue
These databases are generated and downloaded by non-sql-savvy users, so I would like to just give them a query to copy and paste. They will obviously have to start by importing or linking one DB [in]to another.
I'm guessing I will have to make a third table with the combined results query and then delete the other two. Ideally, though, it would just add database A's rows to database B's (overriding where necessary).
I'm of course not looking for a complete answer, just hoping for some advice on where to start. I have some mySQL experience and understand the basics of joins. Is it possible to do this all in one query, or will I have to have a separate one for each table?
THANKS!!

How about:
SELECT t.ID, t.Field1, t.Field2 INTO NewTable FROM
(SELECT a.ID, a.Field1, a.Field2
FROM Table1 A
UNION
SELECT x.ID, x.Field1, x.Field2
FROM Table1 x IN 'C:\docs\db2.mdb'
WHERE x.ID NOT IN (SELECT ID From Table1)) t

This isn't an SQL solution, but may work just as well as telling your non-sql savvy users to cut and paste SQL statements.
I suggest defining a unique Key on the table that takes precedence (col1).
Then copy all the data from Database B into the 'master' table.
This will fail for all duplicates, but insert any 'new' records.
Remove the unique key constraint after you're done if necessary.
From your question it looks like you need the resulting table in database B. So you may want to have your users copy the table into database B before you start or after you're done.

Related

Joining Different Database Tables

I have two tables in Access pulling from databases. There is no primary key linking the two tables, because the databases pull from different programs.
Using SQL, I need all of the information from both tables to pull into a query, and this is where I have problems. The two tables are pulling the same data, but they column titles might not necessarily be the same. For now, I'm assuming they are. How can I get it so that the data from both tables pull into the correct column together?
Here's an example of code (I can't post the real code for certain reasons):
SELECT system1_vehiclecolor, system1_vehicleweight, system1_licenseplate, system2_vehiclecolor, system2_vehicleweight, system2_licenseplate
FROM system1, system2
To further explain this, I want the table to have a column for vehiclecolor, vehicleweight, and licenseplate that combines all of the information. Currently, the way I have it, it is making a column for each of the names in each table, which isn't what I want.
You can use 2 queries to get this done
Select col1as c1 ,col2 col as c2 into resulttable from table1
Insert into resulttable (c1,c2) select colX as c1, colY as c2 from table2
Hope this will help you

Modifying column in access

I have 2 tables in MS Access, TableA and TableB. Table A has only 1 field: myFieldID, and TableB has only 1 field: myFieldName (In reality I have more fields, but these are the ones that matter for the sake of my problem).
Both tables have records that mean the same thing, but written in a different, but similar way.
For instances TableA has:
|TableA.myFieldId |
|-----------------|
|MM0001P |
|HR0003P |
|MH0567P |
So as you can see all of the records are formated this way (with a P at the end):
([A-Z][A-Z][0-9][0-9][0-9][0-9]P)
then, TableB has:
|TableB.myFieldName |
|--------------------------------------------|
|MH-0567 Materials Handling important Role |
|MM-0001 Materials Management Minor Role |
|HR-0003 Human Resources Super Important Role|
So this one has the format (without 'P' at the end):
([A-Z][A-Z]-[0-9][0-9][0-9][0-9] ([A-Z]|[a-z]*))
First, I would like to make join queries with tableA and tableB on these fields, but as you can see, results will be NULL every time since both fields have completely different records.
So I would like to change every name in TableA.myFieldId with his corresponding name in TableB.myFieldName
Problem is, that both tables have around 1 million records, and the fields are repeated multiple times in both tables, plus I don't know how to do this (MS Access doesn't even let me use Regular Expressions).
I would make a table (or query, if it changes often enough) of all unique entries in the 2nd table and the corresponding key for the 1st table. Then use that table or query to help join the two tables.
Something like
Select myFieldName as FName, left(myFieldName,2) & mid(myFieldName,4,4) & "P" as FID
from TableB
group by FName, FID
Important note - are all IDs found in both files, or do you have records in either table that are not in the other? If they don't always match, you may need additional logic or steps to make a master table from both tableA and tableB.

SQL 2012 Data importing and merging data

I am doing a project on database programming using SQL Server 2012 and also visual studio. I have created some tables and I have a excel file with lots of data. The specification at this stage is to merge two excel sheets of data that has 11 columns each (same columns for both files with different data) into a separate table that is then used later on in the project for paging etc.
My original vision was to create two tables, one table that had one excel tab of data and another table exactly the same except for the name to house the second data set, and then using a union join to merge the two tables into one. However importing straight to the tables is impossible (possibly due to the existence of a composite key in column 1 of the data) so I then created two new tables altogether that now does contain the data from the excel sheets however this doesn't meet the specification of merging the files into the table (as the data is still in two tables not one, and also it has to be in a certain table that was created by DDL earlier). Also it doesn't solve the problem as there doesn't seem to be a way to query those tables into an existing table (or is there?)
Anyway, thanks for reading, hopefully I have included enough information, if it seems I've missed something, feel free to ask. I think the ideal solution for this would involve joins of some description such as union but there doesn't seem to be anyway to then relate that join back to the existing table.
Basically, you will want to do something similar this:
SET IDENTITY_INSERT MainTable ON
INSERT INTO MainTable (col1, col2, col3 ... col11)
SELECT col1, col2, col3 ... col11
FROM Table1
UNION
SELECT col1, col2, col3 ... col11
FROM Table2
SET IDENTITY_INSERT MainTable OFF
You haven't specifically mentioned you have an IDENTITY field for your Primary Keys but I'm assuming you are, hence including the SET IDENTITY_INSERT commands.

Compare two tables and save the difference in a file

I have two tables in two different Oracle databases, they look the same (same column names etc) but the data is mostly different. I would like to compare them and save the difference in a third database (or just save it in an easily imported format).
The tables aren't huge but its still like 40 million rows in each table and would like help to do the compare in an efficient way.
There is no keys or unique columns but there are no columns with the same Nr and Name
Table:
Nr Name AText
1234 Jon Doe Ksjfkjsdkfjksdfsf
3234 Jon Sho sdfsdfasdfsdf
1434 Ian Doe lksjdfkljlkjsdfkj
If you're not trying to do this programmatically, you should take a look at SQL Data Compare from Red Gate. I believe it does exactly what you're looking for.
Depends on what you want to find.
For example, if tables are very simmilar, you can make two exports to txt files but ordered(select * from table order by 1, 2, 3) and then try a diff -h between these files. This is somehow fast.
Or, you can import one table in the other database, and try minus, but this is slow. Advantage: you can minus (col1, col2) and exclude col3...

merge msaccess tables

How can i merge two identical tables of two msaccess identical db? For eg:
db1..table1
ID Name
1 Sanjana
2 Parul
3 Rohan
db2...table1
ID Name
1 Sarika
2 Deepak
I want to append the values of second table into first as follows:
ID Name
1 Sanjana
2 Parul
3 Rohan
4 Sarika
5 Deepak
The datatype for the ID field appears to be an autonumber. As such you can do the following:
INSERT INTO db1...table1
SELECT Name FROM db2...table1
You can use an append query:
INSERT INTO Table1 ( FName ) IN 'c:\docs\ltd.mdb'
SELECT A.FName
FROM A;
OK, heres an approach more suited to a beginner making use of the gui.
Backup both databases and store them away somewhere safe.
Do a compact and repair from the tools menu on both databases
Create a linked table in db1 pointing to the table in db2
To do this right click on some white space in the table view of database window and choose link table... Follow the wizard to select db2 and then select table1.
Use an append query to append the data from the linked table1 into the db1.table1
Click into the queries view of Access, create a new query in design view, change its type to Append (right click in free space where the tables appear and go to type->append) Then choose db1.table1 when prompted as the table to append to. Now add the linked table1 into the query, select the fields from which you want to take data (in the example it would just be Name). Note you do not want to take the id field across as this will need to be updated to follow on from where db1.table1 left off, im assuming this is set to autonumber.
Delete the linked table from db1
Im not 100% certain the sort order will be retained from db2.table1 when its appended to db1.table1 as in your example. In most database designs this wont be important but if it is someone else may be able to shed light - i imagine that if the ID field in both tables is also the primary key it will.
Well since it is access, you have two ways. the first is mentioned by LSFR Consulting
and the second would be to use the import wizard and tell the import to ignore the primary key column. That would merge the data from db2 into DB1 without having a primary key collision.
If this is a one time operation simple copy-n-paste will work.
Open both databases in MS Access. Open both tables. Select values to copy (right-click on Column header and Ctrl+C).
Proceed to target table. Make Name field selected in the last row (new record). For this purpose mouse over Name column left edge (cursor becomes a plus sign) and click to select the cell. Ctrl+V. Done.