在Access中同时执行多个更新

时间:2013-10-15 15:04:11

标签: sql ms-access updates

我对访问的工作方式知之甚少,但我需要的功能比我现在所做的更有效。

我有这些疑问:

UPDATE [Receipt Audit]
SET [Receipt Audit].[Receipt Date] = #04/07/2003#
WHERE ((([Receipt Audit].[Receipt Date])=#4/7/303#));

UPDATE [Receipt Audit]
SET [Receipt Audit].[Receipt Date] = #2/27/2004#
WHERE ((([Receipt Audit].[Receipt Date])=#2/27/404#));

UPDATE [Receipt Audit]
SET [Receipt Audit].[Receipt Date] =#5/29/2003#
WHERE ((([Receipt Audit].[Receipt Date])=#5/29/303#));

UPDATE [Receipt Audit]
SET [Receipt Audit].[Receipt Date] =#8/25/2003#
WHERE ((([Receipt Audit].[Receipt Date])=#8/25/303#));

UPDATE [Receipt Audit]
SET [Receipt Audit].[Receipt Date] = #8/28/2003#
WHERE ((([Receipt Audit].[Receipt Date])=#8/28/303#));

UPDATE [Receipt Audit]
SET [Receipt Audit].[Receipt Date] = #9/29/2003#
WHERE ((([Receipt Audit].[Receipt Date])=#9/29/303#));

UPDATE [Receipt Audit]
SET [Receipt Audit].[Receipt Date] = #2/25/2004#
WHERE ((([Receipt Audit].[Receipt Date])=#2/25/404#));

UPDATE [Receipt Audit]
SET [Receipt Audit].[Receipt Date] = #3/30/2004#
WHERE ((([Receipt Audit].[Receipt Date])=#3/30/404#));

UPDATE [Receipt Audit]
SET [Receipt Audit].[Receipt Date] = #8/23/2004#
WHERE ((([Receipt Audit].[Receipt Date])=#8/23/404#));

UPDATE [Receipt Audit]
SET [Receipt Audit].[Receipt Date] = #8/25/2004#
WHERE ((([Receipt Audit].[Receipt Date])=#8/25/404#));

UPDATE [Receipt Audit]
SET [Receipt Audit].[Receipt Date] = #8/26/2004#
WHERE ((([Receipt Audit].[Receipt Date])=#8/26/404#));


UPDATE [Receipt Audit]
SET [Receipt Audit].[Receipt Date] = #8/27/2004#
WHERE ((([Receipt Audit].[Receipt Date])=#8/27/404#));

UPDATE [Receipt Audit]
SET [Receipt Audit].[Receipt Date] = #8/30/2004#
WHERE ((([Receipt Audit].[Receipt Date])=#8/30/404#));

问题是我必须单独运行它们。有没有办法将它们组合成一个查询?任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:2)

也许是这样的?

UPDATE [Receipt Audit]
SET [Receipt Date] = DateSerial(Switch(Year([Receipt Date])=303,2003,Year([Receipt Date])=404,2004,True,Year([Receipt Date])),Month([Receipt Date]),Day([Receipt Date]))
WHERE Year([Receipt Date]) IN (303,404)

答案 1 :(得分:1)

不幸的是,Microsoft Access无法同时运行多个查询。你有两种我能想到的方法。

  • [写一个宏]或VBA程序1
  

您可以创建一个宏来执行特定的一系列操作,以及   您可以创建一个宏组来执行相关的一系列操作。

     

在Microsoft Office Access 2007中,宏可以包含在宏中   对象(有时称为独立宏),或者可以嵌入它们   进入表单,报表或控件的事件属性。嵌入式   宏成为它们所在的对象或控件的一部分   嵌入式。宏导向对象在导航窗格中可见   宏;嵌入式宏不是。

然而,另一条路线可能会好得多。因为您不必在Access中运行宏。

  • 编写查询程序:

这样你就可以编写一个物理连接到数据库的程序;然后程序可以做繁重的工作。这将使您对它有更多的控制权。然后一个实际的编程语言将被操纵。

不确定是否有帮助,我实际上有一个类似的示例项目 - 我会将它发布到Git并在这里为你编辑。

答案 2 :(得分:0)

以下查询可能很容易在某一年内运行:

UPDATE [Receipt Audit] AS R
SET R.[Receipt Date] = 
DateSerial(
        2004,
        Month(R.[Receipt Date]),
        Day(R.[Receipt Date])
        );
WHERE Year(R.[Receipt Date]) = 404

只需更改您想要更改的每年DateSerial的where子句和年份部分。

如果Year(R.[Receipt Date]) = 404不起作用,请尝试:

WHERE Right(R.[Receipt Date],3) = '404'