简体   繁体   English

加快 Access 前端与 SQL Server 后端中整数日期的比较

[英]Speed up comparison of Integer dates in Access front end to SQL Server back end

I'm using Microsoft Access as a front end to a SQL Server data warehouse.我使用 Microsoft Access 作为 SQL Server 数据仓库的前端。 I need to speed up date comparisons in the front end.我需要加快前端的日期比较。 The dates are stored as integers in the back end (eg today is 20191123).日期在后端存储为整数(例如今天是 20191123)。 In the Access front end I convert the integer dates to date data type:在 Access 前端,我将整数日期转换为日期数据类型:

CDate(Right([eventDate],2) & "-" & Mid([eventDate],5,2) & "-" & Left([eventDate],4))

That lets me do date comparisons.这让我可以进行日期比较。 For example, to find occurrences of eventDate between today and 1 month from today I put this in the WHERE clause:例如,要查找从今天到今天起 1 个月之间发生的 eventDate,我将其放在 WHERE 子句中:

Between Date() And DateAdd("m",1,Date())

I need a faster way of doing this.我需要一种更快的方法来做到这一点。 My Access queries are reasonably fast without the date comparison in a WHERE clause, but as soon as I add the date comparison a query that previously ran in a fraction of a second takes 10-15 seconds.我的 Access 查询在 WHERE 子句中没有日期比较的情况下相当快,但是一旦我添加日期比较,以前在几分之一秒内运行的查询需要 10-15 秒。

您可以将您的访问日期转换为表字段的整数格式,然后使用:

Between Val(Format(Date(), "yyyymmdd")) And Val(Format(DateAdd("m",1,Date()), "yyyymmdd"))

Utilize SQL Server SQL engine which is waaay faster than Access!利用比 Access 更快的 SQL Server SQL 引擎!

Create pass-through query in Access, then pass the parameters in "ODBC Connect Str" and copy/paste your Access SQL to this query.在 Access 中创建传递查询,然后在“ODBC Connect Str”中传递参数并将您的 Access SQL 复制/粘贴到此查询。

If the connection string is right and if the SQL syntax is correct, it will be executed on the SQL Server side and it should be a lot faster.如果连接字符串正确,并且 SQL 语法正确,则会在 SQL Server 端执行,速度应该会快很多。 I mean a LOT.我的意思是很多。

Your Connection string should look like this:您的连接字符串应如下所示:

ODBC;DRIVER={SQL Server Native Client 11.0};SERVER=NameOfTheServer;DATABASE=NameOfTheDatabase;UID=Username;PWD=Password ODBC;DRIVER={SQL Server Native Client 11.0};SERVER=NameOfTheServer;DATABASE=NameOfTheDatabase;UID=用户名;PWD=密码

Change the name of the driver if you are using a different one.如果您使用不同的驱动程序,请更改驱动程序的名称。

Note: you might want to change functions for date as those are not the same as in Access ie Date() in Access will be GETDATE() in SQL Server.注意:您可能希望更改日期函数,因为这些函数与 Access 中的不同,即 Access 中的 Date() 将是 SQL Server 中的 GETDATE()。 CDate should be replaced with CAST or CONVERT, etc. CDate 应替换为 CAST 或 CONVERT 等。

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

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