将查询从SQL Server转换为Access 2000

时间:2015-08-08 18:08:30

标签: sql-server ms-access ms-access-2010

我有一个Access 2000 .mdb文件。我在同一个数据库中但在SQL Server上有此查询。查询在SQL Server中工作正常,但在Access中我得到一个错误;我认为这是由于ROW_NUMBER()

我无法使用vba进行此查询,因为我必须使用Odbc驱动程序运行它。

SELECT *
FROM 
    (SELECT 
         *, 
         ROW_NUMBER() OVER (ORDER BY cognome,nome ASC) AS RowID
     FROM 
         (SELECT DISTINCT 
              (a.ID_PAZIENTE) AS codice,
              a.NOME AS nome,
              a.COGNOME AS cognome,
              a.TITOLO AS titolo,
              a.TELEFONOABITAZIONE AS tel,
              a.TELEFONOUFFICIO AS uff,
              a.FAX AS cell,
              a.E_MAIL AS email,
              a.SESSO AS sesso
          FROM 
              PAZIENTI AS a
          WHERE 1 = 1) AS AnagraficheDistinct
    ) AS Anagrafiche
WHERE 
    RowID >= 1 AND RowID <= 25
ORDER BY 
    cognome, nome ASC;

这就是错误:

  

查询表达式中的语法错误(缺少运算符):&#34; ROW_NUMBER()   OVER(由Cognome,nome ASC订购)

我尝试按照在线文档转换查询,这就是我所做的:

SELECT *
    FROM (
  SELECT Top 10
  *
  FROM
  (
   SELECT DISTINCT TOP 20 p.ID_PAZIENTE,p.cognome,p.nome
   FROM PAZIENTI p   
   ORDER BY p.cognome ASC,p.nome ASC
   ) AS sub1
   ORDER BY sub1.cognome DESC,sub1.nome DESC
   ) AS anagrafiche
   ORDER BY  p.cognome ASC,p.nome ASC

不幸的是,此查询会返回奇怪的结果。更改起始索引(How do I implement pagination in SQL for MS Access?),有时会返回10个结果(页面大小),有时基于起始索引(startpos +页面大小)返回12个。

我做错了什么或者这个东西在没有VBA的情况下是不可能的?

由于

2 个答案:

答案 0 :(得分:1)

请考虑以下事项。 RowNumber()在Access SQL中不可用,但可以使用子查询按名称进行复制:

SELECT * 
FROM 
  (SELECT DISTINCT 
             a.ID_PAZIENTE AS codice, 
             a.NOME AS nome, 
             a.COGNOME AS cognome, 
             a.TITOLO AS titolo, 
             a.TELEFONOABITAZIONE AS tel, 
             a.TELEFONOUFFICIO AS uff, 
             a.FAX AS cell, 
             a.E_MAIL AS email, 
             a.SESSO AS sesso,
             (SELECT count(*)
              FROM PAZIENTI As b
              WHERE b.cognome <= a.cognome 
                AND b.nome <= a.nome) As RowID
   FROM PAZIENTI AS a) AS AnagraficheDistinct 
   ) AS Anagrafiche 
WHERE RowID >= 1 AND RowID <= 25 
ORDER BY cognome, nome;

答案 1 :(得分:0)

首先,MS-Access中的查询在逻辑上与SQL-Server-Query完全不同。

第一个为您提供cognome,nome排序的前25行。 Access-Query以指定的顺序为您提供前20行的最后10行。

在Access中没有内置函数,例如ROW_NUMBER。并且因为您使用两个字段作为ROW_NUMBER(cognome,nome)的排序条件,所以Count(*)子查询的使用不起作用。

但是我已经在Access中构建了一种RANK-Function,也许这对你有用。但是,如果cognome中具有相同值的行数大于pagesize,则在指定一个页面大小时,它可能会返回更多行。在此查询中,重要的是,只有cognome是Count(*)子查询的一部分。

SELECT p.*
FROM
(
  SELECT codice, cognome, nome, titolo, tel, uff, cell, email
  FROM
  (
    SELECT DISTINCT a.ID_PAZIENTE AS codice, a.COGNOME AS cognome, 
                    a.NOME AS nome, a.TITOLO AS titolo, 
                    a.TELEFONOABITAZIONE AS tel, a.TELEFONOUFFICIO AS uff,  
                    a.FAX AS cell, a.E_MAIL AS email, a.SESSO AS sesso, 
                    (SELECT Count(*) FROM PAZIENTI AS temp 
                     WHERE temp.cognome < a.cognome
                    ) + 1 AS Rank
    FROM PAZIENTI AS a
  ) AS psub
WHERE Rank >= 1 AND Rank <= 5
ORDER BY Rank
) p
ORDER BY cognome, nome

如果这对您没有帮助,我想,您将需要一个VBA功能。但是,在每次查询之前重置计数器至关重要。

另一个选项是使用Pass-Through-Query并将第一个查询直接发送到SQL-Server。您必须在Access中的Query-Object中指定odbc-Connectionstring。