简体   繁体   English

将MERGE与MS-Access和SQL Server 2008一起使用

[英]Using MERGE with MS-Access and SQL Server 2008

So I really don't know a lot about coding or SQL at all. 所以我真的对编码或SQL一点也不了解。 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. 将数据分组在一起后,我需要将其上传回SQL Server。

Access table: table1 存取表: table1

serial# cust# cust_name order# model#

SQL Server table: dbo_Data1 SQL Server表: 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. 我在SQL Server中拥有除order#model#所有内容,并且可以在Access中找到所有信息。 I just need to upload my Access table into SQL Server. 我只需要将Access表上传到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. 我一直在读, MERGE是最好的方法,而且是分批进行的,但是我不知道该怎么做。 Do I write a query using SQL view and use this http://technet.microsoft.com/en-us/library/bb510625.aspx format? 我是否使用SQL视图编写查询并使用此http://technet.microsoft.com/zh-cn/library/bb510625.aspx格式? and that just ignores access update/maketable/append query types? 只是忽略访问更新/ maketable /追加查询类型?

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. 另外,我不能只删除旧数据,因为我想避免从1998年开始为更新而提取数据,并且需要将该数据保留在服务器上。 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). 同样,执行此更新的旧方法是仅使用Access附加查询,如果每周执行一次,则该查询可能要花费10个小时(并且在良好的一年中还没有完成)。 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. 我想避免10个小时的更新,因为我一天只工作8个小时,并且没有一台额外的计算机可以让我在Access工作期间保持忙碌状态。

Can anyone shed some light on this for me? 谁能为我阐明一下? My main question is just how does MERGE work?? 我的主要问题是MERGE如何工作?

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. MERGE是Transact-SQL的功能,因此,如果要使用它,则必须在Access中运行通过查询。 Here's how you would do that: 这是您要执行的操作:

Say that you have a "main" [ExchangeRates] table on the SQL Server with the following data: 假设您在SQL Server上具有一个“主” [ExchangeRates]表,其中包含以下数据:

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. 您在SQL Server上还有一个名为[ExchangeRateUpdates]的表,该表具有相同的结构。 You have that table defined in Access as a linked table named [dbo_ExchangeRateUpdates]. 您已在Access中将该表定义为名为[dbo_ExchangeRateUpdates]的链接表。

You also have a local working table named [LocalTable] in Access, again with the same structure. 您在Access中还具有一个名为[LocalTable]的本地工作表,该表也具有相同的结构。 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 因此,在完成本地所需的任何处理(为了达到最大速度)之后,您的[LocalTable]将包含

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: 您可以通过在Access中运行三个查询将这些更改合并到SQL Server的主表中:

Query 1: A Delete query to empty out any previous updates from the [ExchangeRateUpdates] table 查询1:删除查询以清空[ExchangeRateUpdates]表中以前的所有更新

DELETE FROM dbo_ExchangeRateUpdates;

Query 2: An Append query to upload the current updates to the [ExchangeRateUpdates] table 查询2:一个追加查询,用于将当前更新上传到[ExchangeRateUpdates]表

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 查询3:通过查询将[ExchangeRateUpdates]表中的更新合并到SQL Server的“主” [ExchangeRates]表中

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 .) (注意:在定义传递查询时,请确保将其Returns Records属性设置为No

When that is done the "main" [ExchangeRates] table will contain 完成后,“主” [ExchangeRates]表将包含

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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