简体   繁体   中英

Using MERGE with MS-Access and SQL Server 2008

So I really don't know a lot about coding or SQL at all. I'm used to access but not a pro or veteran at it. I have data coming from a server and im using queries to add in local database info. after the data is grouped together I need to upload it back to the SQL Server.

Access table: table1

serial# cust# cust_name order# model#

SQL Server table: dbo_Data1

serial# cust# cust_name order# model#

I have everything but order# and model# in SQL Server and can find all info in Access. I just need to upload my Access table into SQL Server. I keep reading that MERGE is the best way to do this and in batches, but I don't understand how to do this. Do I write a query using SQL view and use this http://technet.microsoft.com/en-us/library/bb510625.aspx format? and that just ignores access update/maketable/append query types?

Also I can't just erase the old data because I want to avoid pulling data from 1998 for the update and need that data to stay on the server. Also the old way of doing this update was to just use an Access append-query which supposedly took 10 hours if done weekly (and hasn't been done for a good year). I want to avoid a 10 hour update since I only work 8 hours a day and don't have an extra computer to keep me busy while Access works.

Can anyone shed some light on this for me? My main question is just how does MERGE work??

Thanks.

MERGE is a Transact-SQL feature, so if you want to use it you will have to run a Pass-Through query from within Access. Here's how you would do that:

Say that you have a "main" [ExchangeRates] table on the SQL Server with the following data:

CurrencyName   CanadianDollarEquivalent
-------------  ------------------------
European Euro                    1.3729
U. S. Dollar                          1

You also have a table on the SQL Server named [ExchangeRateUpdates] that has the identical structure. You have that table defined in Access as a linked table named [dbo_ExchangeRateUpdates].

You also have a local working table named [LocalTable] in Access, again with the same structure. Let's imagine that after a whole bunch of "number crunching" you determine that the updates you want to apply to the "main" table on the server are:

  • you want to update the "US Dollar" exchange rate because the two currencies are no longer at par, and

  • you want to add an exchange rate for the Australian Dollar

So, after doing whatever processing is required locally (for maximum speed) your [LocalTable] will contain

CurrencyName       CanadianDollarEquivalent
-----------------  ------------------------
U. S. Dollar                          1.047
Australian Dollar                    0.9622

You can merge those changes into the main table on the SQL Server by running three queries in Access:

Query 1: A Delete query to empty out any previous updates from the [ExchangeRateUpdates] table

DELETE FROM dbo_ExchangeRateUpdates;

Query 2: An Append query to upload the current updates to the [ExchangeRateUpdates] table

INSERT INTO dbo_ExchangeRateUpdates
SELECT * FROM LocalTable;

Query 3: A Pass-Through query to merge the updates in the [ExchangeRateUpdates] table into the "main" [ExchangeRates] table on the SQL Server

MERGE dbo.ExchangeRates AS target
USING dbo.ExchangeRateUpdates AS source
ON (target.CurrencyName = source.CurrencyName)
WHEN MATCHED THEN
    UPDATE SET CanadianDollarEquivalent = source.CanadianDollarEquivalent
WHEN NOT MATCHED THEN
    INSERT (CurrencyName, CanadianDollarEquivalent)
    VALUES (source.CurrencyName, source.CanadianDollarEquivalent);

(Note: When defining the Pass-Through query, be sure to set its Returns Records property to No .)

When that is done the "main" [ExchangeRates] table will contain

CurrencyName       CanadianDollarEquivalent
-----------------  ------------------------
European Euro                        1.3729
U. S. Dollar                          1.047
Australian Dollar                    0.9622

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM