简体   繁体   English

ms 访问查询(ms 访问冻结)

[英]ms access query (ms access freezes)

I have this report and need to add totals for each person (the red circle) existing report我有这份报告,需要为每个人(红色圆圈)现有报告添加总数

new report新报告

I cannot change the existing report so I export data from MS SQL to MS Access and create a new report there.我无法更改现有报告,因此我将数据从 MS SQL 导出到 MS Access 并在那里创建新报告。 I got it working for one employee but have trouble with a query which would for multiple employees.我让它为一名员工工作,但在查询多名员工时遇到了麻烦。

This query extract data use as input:此查询提取数据用作输入:

SELECT [TIME].[RCD_NUM], [TIME].[EMP_ID], [TIME].[PPERIOD], [TIME].[PRUN], [TIME].[TDATE], [TIME].[PC], [TIME].[RATE], [TIME].[HOURS], [TIME].[AMOUNT], [TIME].[JOB_ID], [TIME].[UPDATED], [TIME].[UPDATED_BY], [TIME].[LOG_DATE], [TIME].[ORIGINAL_REC_NUM]
FROM [TIME]
WHERE ((([TIME].[EMP_ID])=376) And (([TIME].[TDATE])<=#12/31/2006# And ([TIME].[TDATE])>=#1/1/2006#) And (([TIME].[PC])<599));

this query populates the report:此查询填充报告:

SELECT *
FROM TIME1
WHERE RCD_NUM = (SELECT Max(RCD_NUM) FROM [TIME1] UQ WHERE UQ.PPERIOD = [TIME1].PPERIOD AND UQ.PC = [TIME1].PC);

the problem is if I remove EMP_ID from the first query like this问题是如果我从这样的第一个查询中删除 EMP_ID

SELECT [TIME].[RCD_NUM], [TIME].[EMP_ID], [TIME].[PPERIOD], [TIME].[PRUN], [TIME].[TDATE], [TIME].[PC], [TIME].[RATE], [TIME].[HOURS], [TIME].[AMOUNT], [TIME].[JOB_ID], [TIME].[UPDATED], [TIME].[UPDATED_BY], [TIME].[LOG_DATE], [TIME].[ORIGINAL_REC_NUM]
FROM [TIME]
WHERE ((([TIME].[TDATE])<=#12/31/2006# And ([TIME].[TDATE])>=#1/1/2006#) And (([TIME].[PC])<599));

then the second query doesn't work and ms access freezes when running this query.然后第二个查询不起作用,并且运行此查询时 ms 访问冻结。

any help/idea please?请问有什么帮助/想法吗?

Caveat : I won't pretend to know the precise cause of the problem, but I have had to repeatedly refactor queries in Access to get them working even though the original SQL statements are completely valid in regards to syntax and logic.警告:我不会假装知道问题的确切原因,但我不得不反复重构 Access 中的查询以使它们工作,即使原始 SQL 语句在语法和逻辑方面完全有效。 Sometimes I've had to convolute a sequence of queries just to avoid bugs in Access.有时,为了避免 Access 中的错误,我不得不对一系列查询进行卷积。 Access is often rather dumb and will simply (re)execute queries and subqueries exactly as given without optimization. Access 通常是相当愚蠢的,并且会简单地(重新)执行完全按照给定的查询和子查询,而无需优化。 At other times Access will attempt to combine queries by performing some internal optimizations, but sometimes those introduce frustrating bugs.在其他时候,Access 会尝试通过执行一些内部优化来组合查询,但有时这些会引入令人沮丧的错误。 Something as simple as a name change or column reordering can be the difference between a functioning query and one that crashes or freezes Access.像名称更改或列重新排序这样简单的事情可能是正常运行的查询与崩溃或冻结 Access 的查询之间的区别。


First consider:首先考虑:

  • Can you leave the data on SQL Server and link to the results in Access (rather than export/importing it into Access)?您能否将数据保留在 SQL Server 上并链接到 Access 中的结果(而不是将其导出/导入 Access)? Even if you need or prefer to use Access for creating the actual report, you could use all the power of SQL Server for querying the data--it is likely less buggy and more efficient.即使您需要或更喜欢使用 Access 来创建实际报告,您也可以使用 SQL Server 的所有功能来查询数据——它可能更少错误且更高效。
    • Common best practice is to create SQL Server stored procedures that return just what data you need in Access.常见的最佳做法是创建 SQL Server 存储过程,这些过程仅返回您在 Access 中需要的数据。 A pass-through query is created in Access to retrieve the data, but all data operations are performed on the server.在 Access 中创建传递查询来检索数据,但所有数据操作都在服务器上执行。
  • Perhaps this is just a performance issue where limiting the set by [EMP_ID] selects a small subset, but the full table is large enough to "freeze" Access.也许这只是一个性能问题,其中通过[EMP_ID]限制集合选择了一个小的子集,但整个表足够大以“冻结”访问。
    • How long have you let Access remain frozen before killing the process?在终止进程之前,您让 Access 保持冻结状态多久了? Be patient... like many, many minutes (or hours).要有耐心......就像很多很多分钟(或几小时)。 Start it in the morning and check after lunch.早上开始,午饭后检查。 :) It might eventually return a result set. :) 它最终可能会返回一个结果集。 This does not imply it is tolerable or that there is no other solution, but it can be useful to know if it eventually returns data or not.这并不意味着它是可以容忍的或没有其他解决方案,但了解它最终是否返回数据可能很有用。
    • How many possible records are there?有多少可能的记录?
    • Are the imported data properly indexed?导入的数据是否正确索引? Add indexes to all key fields and those which are used in WHERE clauses.为所有关键字段和 WHERE 子句中使用的字段添加索引。
    • Is the database located on a network share or is it local?数据库是位于网络共享还是本地? Try copying the database to a local drive.尝试将数据库复制到本地驱动器。

Other hints:其他提示:

  • Try the BETWEEN operator for dates in the WHERE clause.对 WHERE 子句中的日期尝试 BETWEEN 运算符。

Try refactoring the "second" query by performing a join in the FROM clause rather than the WHERE clause.尝试通过在 FROM 子句而不是 WHERE 子句中执行连接来重构“第二个”查询。 In doing this, you may also want to save the subquery as a named query (just as [TIME1] is saved).这样做时,您可能还想将子查询保存为命名查询(就像保存[TIME1] )。 Whether or not a query is saved or embedded in another statement CAN change the behavior of Access (see caveat) even though the results should be identical.查询是否被保存或嵌入到另一个语句中都可以改变 Access 的行为(见警告),即使结果应该是相同的。

Here's a version with the embedded aggregate query.这是一个带有嵌入式聚合查询的版本。 Notice how all column references are qualified with the source.请注意所有列引用如何使用源进行限定。 Some of the original query's columns do not have a source alias prefixing the column name.某些原始查询的列在列名前没有源别名。 Remember the caveat... such picky details can affect Access behavior.:记住警告...这样挑剔的细节会影响访问行为。:

SELECT TIME1.*
FROM TIME1 INNER JOIN
  (SELECT UQ.PPERIOD, UQ.PC, Max(UQ.RCD_NUM) As Max_RCD_NUM
   FROM [TIME1] UQ
   GROUP BY UQ.PPERIOD, UQ.PC) As TIMEAGG
  ON (TIME1.PPERIOD = TIMEAGG.PPERIOD) And (TIME1.PC = TIMEAGG.PC)
    AND (TIME1.RCD_NUM = TIMEAGG.Max_RCD_NUM)

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

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