具有多个参数的MS Query

时间:2013-03-04 15:36:41

标签: sql-server-2012 ms-query excel-2013

我有这个SQL查询,我希望结果在Excel 2013工作表/表

SELECT 
  [MS Part Number], [Item Name], SUM([Aantal]) AS Aantal, [Prijs]
FROM 
  (
    SELECT 
      [MS Part Number], [Item Name], SUM([Aantal]) AS Aantal, [Prijs]
    FROM 
      vwSPLAInformatie
    WHERE 
      (DATEPART(YEAR, fakdat) = '2013') 
      AND (DATEPART(QUARTER, fakdat) = '1')
      AND docnumber LIKE '%kwartaal%'
    GROUP BY 
      [MS Part Number], [Item Name], [Aantal], [Prijs]
    UNION ALL
    SELECT 
      [MS Part Number], [Item Name], SUM([Aantal]) AS Aantal, [Prijs]
    FROM 
      vwSPLAInformatie
    WHERE 
      (DATEPART(YEAR, fakdat) = '2013') 
      AND (DATEPART(MONTH, fakdat) = '1')
      AND NOT docnumber LIKE '%kwartaal%'
    GROUP BY 
      [MS Part Number], [Item Name], [Aantal], [Prijs]
  ) AS Basis
GROUP BY 
  [MS Part Number], [Item Name], [Prijs]

它工作得很好,唯一的是我有4个参数,我想连接到Excel中的3个单元格值。 (年= 2013)(季度= 1)(月= 2)。但是,当我将查询调整为以下内容时:

 WHERE (DATEPART(YEAR, fakdat) = '?')

我在Excel中选择了一个说2013年的单元格,所以基本上它应该可以工作,但它不会。我收到以下错误:

[Microsoft]ODBC SQL Server][SQL Server]Conversion failed when converting the varchar value '?' to data type in.

值相同,但根本不起作用。我怎样才能解决这个问题?当这个工作时,我会非常高兴!在前进中,非常感谢。

0 个答案:

没有答案