merge msaccess tables - sql

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.

Related

Swapping columns in a table to match formatting of another table prior to row insertion

I want to swap columns within Visual Fox Pro 9 in table_1 before inserting its rows into table_2 so as to avoid data losses caused by datatype variations. I tried these two options based on other solutions on stackoverflow, but I get syntax error messages for both command inputs. The name field is of datatype = character(5)and it needs to be after the subdir field.
ALTER table "f:\csp" modify COLUMN name character(5) after subdir
ALTER table "f:\csp" change COLUMN name name character(5) after subdir
I attempted these commands based on solutions here:
How to move columns in a MySQL table?
You never need to change the column order, and you never should rely on column order to do something.
For inserting into another table from this one you could simply select the columns in the order you desired (and their column names do not even need to be the same in the case of "insert ... select ... "). ie:
insert into table_2 (subdir, name) ;
select subdir, name from table_1
Another way is to use the xBase commands like:
select table_2
append from table_1
In the case of latter, VFP would do the match on column names.
All in all, relying on column ordering is dangerous. If you really want to do that, then you can still do, in a number of ways. One of them is to select all data into a temp table, recreate the table in the order you want and fill back from temp (might not be as easy as it sounds if there are existing dependencies such as referential integrity - also you need to recreate the indexes).

Importing Data From One database to another

I wanted to know that, Suppose I have a table in one database with say 1000 records and I have similar table in another database with say 500 records.
Now my question is If I will try to import data from say DB1.Tbl1 to DB2.Tbl1, then what will happen? Is there any possibilities of duplicacy of the data?
I wanted the records of DB1.Tbl1 to copy into DB2.Tbl1 table. Please clear my confusion.
If you have same data in both tables you can first truncate 2nd table after that you can import data from 1st table by Insert Command or "import data" task
Try this
INSERT INTO DB1.dbo.Tbl1 SELECT * FROM DB2.dbo.Tbl2
This just moves the data. If you want to move the table definition, you have to do something else.
Note that, SQL Server Management Studio's "Import Data" task (right-click on the DB name, then tasks) will do most of this for you. Run it from the database you want to copy the data into.
If the tables don't exist it will create them for you, but you'll probably have to recreate any indexes. If the tables do exist, it will append the new data by default but you can adjust that (edit mappings) so it will delete all existing data.
Try this:
We can copy all columns from one table to another, existing table:
INSERT INTO table2
SELECT * FROM table1;
Or we can copy only the columns we want to into another, existing table:
INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;

Updating all columns of a table

I am using MS Access to connect to a linked Oracle database. I have table B that pulls only certain columns from table A (in the linked DB).
I am trying to execute a macro that runs Query1 to update table B with the data from A that is constantly changing.
The two are not necessarily related by some ID, so the UPDATE TABLE command doesn't seem logical to me. Should I be using JOIN? I just need a place to launch my query from.
Based on the discussion in the comments, it sounds like Table B is simply a "view" on Table A. I am not confident that you need an UPDATE query.
In MS Access, the syntax to create a view is thus:
CREATE VIEW my_view AS
SELECT
col1, col2
FROM
[Table A]

Change the order of database columns

I want to change the order of column e.g. name is first column of my table and there are 10 other columns in table I want to insert a new column in 2nd position after name column.
How is this possible?
1 - It's not possible without rebuilding the table, as Martin rightly points out.
2 - It's a good practice anyways to specify what fields you want and in what order in your SELECT statements as n8wrl points out.
3 - If you really really need a fixed order on your fields, could create a view that selects the fields you want in the order you want.
Like the rows in the table, there is no meaning to the order of the columns. In fact, it is best to specify the order you want the columns in your select statements rather than using select *, so you can 'insert' new columns wherever you want just by writing your SELECT statements accordingly.
Its possible to change the order. In some instances it really matters. have a personal experience.
Anyway..this query works fine.
ALTER TABLE user MODIFY Name VARCHAR(150) AFTER address;
You can achieve this by following these steps:
remove all foreign keys and primary key of the original table.
rename the original table.
using CTAS, create the original table in the order you want.
drop the old table.
apply all constraints back to the original table.

How to filter by 2 fields when loading data into an access database table from an excel spreadsheet

OK, here is my problem, without being too specific for reasons of being fired for putting company practices on the internet.
There are spreadsheets made. These are uploaded to the database. I need to filter out duplicates from uploading. The only way to do this is by making sure that for each and every entry that two fields aren't the same as an entry already in the database. As just becasue one field is the same does not mean its a duplicate. There are two specific fields lets call them FLDA and FLDB that both must match up to an entry in the database already. I can filter by one field already. I'm thinking this has to be a subquery but I'm not sure how to apply it.
This is hard to decribe. Just ask if your not sure what I mean.
I had a similar problem. My solution was to:
import into a staging-table.
delete the duplicates
copy what's left over into the live table
It's a little BFI, but it just plain works.
Would a query suit? For example:
INSERT INTO ToUpdate ( Field1, Field2 )
SELECT e.H1, e.H2
FROM (SELECT * FROM [Sheet1$] IN '' [Excel 8.0;HDR=YES;IMEX=1;database=C:\Docs\LTD.xls]) As e
LEFT JOIN ToUpdate ON (e.H2 = ToUpdate.Field2) AND (e.H1 = ToUpdate.Field1)
WHERE ToUpdate.Field1 Is Null AND ToUpdate.Field2 Is Null
How are you loading them into the database? Is this with your own code to read the Excel files? You can read the Excel files using ADO/ADO.NET with the right connection string. Then you could read the data using a query that would weed out the dupes.
Building on CodeSlave's answer, Access provides a Find Duplicates Query wizard that can help you easily build the query to weed out the duplicates.
Another approach would be to set up an identity on FLDA and FLDB. This will prevent duplicate entries from even getting written to the table. Of course you will also need to catch the exception that is thrown when the insert operation fails.
Is there a field FLDC that would be different for identifying duplicates? I presume there must be, as otherwise having (FLDA,FLDB) as a unique or primary key would solve your problem immediately.
Assuming there is such a field, you could try something like this:
SELECT T1.FLDA, T1.FLDB, T1.FLDC
FROM Table1 T1, Table1 T2
WHERE T1.FLDA = T2.FLDA
AND T1.FLDB = T2.FLDB
AND T1.FLDC <> T2.FLDC
The downside here is that both the original and the duplicate will be returned by something like this. If you only want to see the duplicates, you will probably have to figure out a way to identify an 'original' row and add another WHERE clause or two for this.
If you can get a query that gives you just the duplicate rows and not the originals, it should be pretty easy to change it to a DELETE query.
To avoid duplicates on imports:
1 - If there's not already a primary key on the table, put one on FLDA and FLDB (together). If there's already a primary key that's not FLDA and FLDB (together), place an index on the table on these two fields, unique yes, ignore nulls no.
2 - You can import from the spreadsheet to the table with the wizard or with a query. If you do it with the import spreadsheet wizard, you'll see this message before the import starts:
"DB name was unable to append all the data to the table.
"The contents of fields in 0 records were deleted and (xx) records were lost due to key violations. (These lost records were duplicates, so no real loss there.) ... Do you want to proceed anyway?"
Click yes to import the rows from the spreadsheet. No duplicates will be imported.
Or, to use a query for the import, paste this into a new query in sql view (menu: Insert > Query > Design View, Close button; menu: View > SQL View.)
INSERT INTO tblInput
SELECT XLS.*
FROM tblInput AS T RIGHT JOIN
[Excel 8.0;IMEX=1;HDR=Yes;DATABASE=c:\data.xls;].[Sheet1$] AS XLS
ON T.FLDA = XLS.FLDA AND T.FLDB = XLS.FLDB
WHERE ISNULL(T.FLDA) AND ISNULL(T.FLDB);
Change the path, c:\data.xls, to your path, Sheet1$ to your sheetname, tblInput to your table name, and FLDA and FLDB to your column names. If the spreadsheet doesn't have headers (column names) change HDR=Yes to HDR=No
I did this by using a delete query and then using Select from Table 1 Group by X Having Y, Z, A. And then I put run query button for front end users.
Cheers for all your help.