组合查询时的SQL服务器语法错误(传递查询)

时间:2012-06-13 12:02:41

标签: sql-server ms-access pass-through

我正在尝试将2个查询组合到sql-server传递查询(PTQ)。在Access中单独编码时,两个查询都有效。将这些访问代码组合到一个查询时,它仍然可以在Access中使用。 但是当我将字段名称格式化为T-sql并尝试作为PTQ运行时,我在“GROUP”附近出现语法错误。当我删除这个“GROUP BY”行时,我在“;”附近收到语法错误。

SELECT jaar, 
       maand, 
       wicode, 
       pg_intern_code, 
       pg_kriss_code, 
       pg_kriss_naam, 
       Count(pc0517.dbo.resul.klavnr) AS AantalVanKLAVNR 
FROM   (SELECT Year(pc0517.dbo.resul.created)  AS JAAR, 
               Month(pc0517.dbo.resul.created) AS MAAND, 
               pc0517.dbo.resul.wicode, 
               dbo.tblproductgroep_intern.pg_intern_code, 
               dbo.tblproductgroep_kriss.pg_kriss_code, 
               dbo.tblproductgroep_kriss.pg_kriss_naam, 
               pc0517.dbo.resul.klavnr 
        FROM   (dbo.tblproductgroep_kriss 
                INNER JOIN (dbo.tblproductgroep_intern 
                            INNER JOIN dbo.tblrobbe_pg 
                                    ON dbo.tblproductgroep_intern.pg_intern_id = 
                                       dbo.tblrobbe_pg.pg_code_intern_id) 
                        ON dbo.tblproductgroep_kriss.pg_kriss_id = 
                           dbo.tblrobbe_pg.pg_code_kriss_id) 
               INNER JOIN pc0517.dbo.resul 
                       ON dbo.tblrobbe_pg.robbe_pg_naam = 
                          pc0517.dbo.resul.prcode 
        WHERE  ( ( Year(pc0517.dbo.resul.created) = 2012 ) 
                 AND ( pc0517.dbo.resul.kanaal = "gg" ) )) 
GROUP  BY jaar, 
          maand, 
          wicode, 
          pg_intern_code, 
          pg_kriss_code, 
          pg_kriss_naam; 

其他PTQ在同一个数据库中(甚至与PC0517数据库结合使用),工作没有任何故障,但我对SQL服务器完全不熟悉,所以我还不知道所有的约定,我可能会忽略一些东西基本...

TIA

1 个答案:

答案 0 :(得分:2)

我纠正了各种各样的事情:(1)以正确的顺序放置连接(2)删除错位的括号(3)  为可读性添加了表别名(4)在"gg"(5)周围删除了双引号,使得针对r.created的查询成为开放式范围,以便可以使用索引(如果存在)。我还从分组中的内部查询中删除了jaar - 基于where子句,除了2012之外,它永远不会是任何其他内容。

如果你将它粘贴到那里,我不知道Access会对此做什么,但你应该能够确认这是一个比通过直接对SQL Server运行的垃圾访问更正确的查询。如果Access仍然打破了这个,我会将它放在SQL Server中的存储过程或视图中,并指向Access。

SELECT 
   jaar = 2012, 
   maand, 
   wicode, 
   pg_intern_code, 
   pg_kriss_code, 
   pg_kriss_naam, 
   COUNT(klavnr) AS AantalVanKLAVNR 
FROM   
(
    SELECT 
       maand = MONTH(r.created), 
       r.wicode, 
       i.pg_intern_code, 
       k.pg_kriss_code, 
       k.pg_kriss_naam, 
       r.klavnr 
    FROM 
       dbo.tblproductgroep_kriss AS k
    INNER JOIN dbo.tblrobbe_pg AS tr 
       ON k.pg_kriss_id = tr.pg_code_kriss_id
    INNER JOIN dbo.tblproductgroep_intern AS i 
       ON i.pg_intern_id = tr.pg_code_intern_id
    INNER JOIN pc0517.dbo.resul AS r 
       ON tr.robbe_pg_naam = r.prcode 
    WHERE 
       r.created >= '20120101' AND r.created < '20130101'
       AND r.kanaal = 'gg'
) AS x 
GROUP BY 
   maand, 
   wicode, 
   pg_intern_code, 
   pg_kriss_code, 
   pg_kriss_naam; 

它也可以简化为:

    SELECT 
       jaar = 2012,
       maand = MONTH(r.created), 
       r.wicode, 
       i.pg_intern_code, 
       k.pg_kriss_code, 
       k.pg_kriss_naam, 
       r.klavnr 
    FROM 
       dbo.tblproductgroep_kriss AS k
    INNER JOIN dbo.tblrobbe_pg AS tr 
       ON k.pg_kriss_id = tr.pg_code_kriss_id
    INNER JOIN dbo.tblproductgroep_intern AS i 
       ON i.pg_intern_id = tr.pg_code_intern_id
    INNER JOIN pc0517.dbo.resul AS r 
       ON tr.robbe_pg_naam = r.prcode 
    WHERE 
       r.created >= '20120101' AND r.created < '20130101'
       AND r.kanaal = 'gg'
    GROUP BY 
       MONTH(r.created), 
       r.wicode, 
       i.pg_intern_code, 
       k.pg_kriss_code, 
       k.pg_kriss_naam; 

(换句话说,子查询似乎没必要。)