Excel:关键字'SELECT'附近的语法不正确

时间:2013-01-31 15:24:23

标签: sql excel ms-query

场景:我正在使用Excel构建报告来计算佣金。这是基于上个月的发票。 我创建了以下tsql查询,并使用MSQuery创建了一个连接。我测试了查询并且它完美无缺,直到我将 Where 语句更改为使用参数"?",所以我可以,然后我收到以下错误:

Incorrect Syntax near the keyword `'SELECT'`

以下是查询:

SELECT v_rpt_Invoices.Invoice_Number, v_rpt_Invoices.Territory, v_rpt_Company.Account_Nbr, v_rpt_Invoices.Company_Name, v_rpt_Invoices.Date_Invoice, 
                      v_rpt_Invoices.Location, v_rpt_Invoices.TicketNbr, v_rpt_Invoices.Project_ID, v_rpt_Invoices.Invoice_Type, v_rpt_Invoices.Status_Description, 
                      CASE WHEN TicketNbr <> 0 THEN 'Service Ticket' WHEN Project_ID IS NOT NULL THEN 'Project' ELSE 'Other' END AS Invoice_For, 
                      CASE WHEN ticketNbr <> 0 THEN
                          (SELECT     v_rpt_Service.Board_Name
                            FROM          v_rpt_Service
                            WHERE      v_rpt_Invoices.TicketNbr = v_rpt_Service.TicketNbr) WHEN Project_ID IS NOT NULL THEN Project_ID ELSE 'Other' END AS Service_Board_Project, 
                      CASE WHEN TicketNbr <> 0 THEN
                          (SELECT     Bill_Method
                            FROM          SR_Service
                            WHERE      v_rpt_Invoices.TicketNbr = SR_Service.SR_Service_RecID) WHEN project_id IS NOT NULL THEN
                          (SELECT     PM_Billing_Method_ID
                            FROM          PM_Project
                            WHERE      v_rpt_Invoices.PM_Project_RecID = PM_Project.PM_Project_RecID) ELSE 'NONE' END AS BillingMethod, v_rpt_Invoices.Invoice_Amount, 
                      CASE WHEN (TicketNbr <> 0 AND
                          (SELECT     Bill_Method
                            FROM          SR_Service
                            WHERE      v_rpt_Invoices.TicketNbr = SR_Service.SR_Service_RecID) = 'A') THEN Invoice_Amount * 0.7 WHEN (TicketNbr <> 0 AND
                          (SELECT     Bill_Method
                            FROM          SR_Service
                            WHERE      v_rpt_Invoices.TicketNbr = SR_Service.SR_Service_RecID) = 'F') THEN 0.01 WHEN (project_id IS NOT NULL AND
                          (SELECT     PM_Billing_Method_ID
                            FROM          PM_Project
                            WHERE      v_rpt_Invoices.PM_Project_RecID = PM_Project.PM_Project_RecID) = 'A') THEN Invoice_Amount * 0.7 WHEN (project_id IS NOT NULL AND
                          (SELECT     PM_Billing_Method_ID
                            FROM          PM_Project
                            WHERE      v_rpt_Invoices.PM_Project_RecID = PM_Project.PM_Project_RecID) = 'F') THEN 0.01 ELSE 0.00 END AS Cost
FROM         v_rpt_Invoices INNER JOIN
                      v_rpt_Company ON v_rpt_Invoices.Company_RecID = v_rpt_Company.Company_RecID
**WHERE     (v_rpt_Invoices.Date_Invoice >= ?)**
order by Territory, Invoice_For

1 个答案:

答案 0 :(得分:0)

删除此行

 **WHERE     (v_rpt_Invoices.Date_Invoice >= ?)**

这不是有效的SQL - 看起来你想要评论使用双短划线。

除非您只是让**显示您所做的更改。在这种情况下,您需要有一个值?是SQL不起作用的值。


作为助手,这个查询可以更加清晰和快速。考虑将子查询合并到连接中。例如,以下子查询

SELECT     Bill_Method
FROM          SR_Service
WHERE      v_rpt_Invoices.TicketNbr = SR_Service.SR_Service_RecID
必须为每一行调用

如果你将它作为连接,你从O(nm)到O(n + m)其中n是v_rpt_Invoices的大小,m是SR_Service的大小。

这只是您可以优化的潜在子查询之一。

以下是在您的示例的子查询中滚动的示例 (我无法测试,因此它可能有错误/错别字)

SELECT 
  v_rpt_Invoices.Invoice_Number,
       v_rpt_Invoices.Territory,
       v_rpt_Company.Account_Nbr,
       v_rpt_Invoices.Company_Name,
       v_rpt_Invoices.Date_Invoice, 
       v_rpt_Invoices.Location,
       v_rpt_Invoices.TicketNbr,
       v_rpt_Invoices.Project_ID,
       v_rpt_Invoices.Invoice_Type,
       v_rpt_Invoices.Status_Description, 
      CASE WHEN TicketNbr <> 0 THEN 'Service Ticket' 
           WHEN Project_ID IS NOT NULL THEN 'Project' 
           ELSE 'Other' 
      END AS Invoice_For, 
      CASE WHEN ticketNbr <> 0 THEN v_rpt_Service.Board_Name
           WHEN Project_ID IS NOT NULL THEN Project_ID 
           ELSE 'Other' 
      END AS Service_Board_Project, 
      CASE WHEN TicketNbr <> 0 THEN SR_Service.Bill_Method
           WHEN project_id IS NOT NULL THEN PM_Project.PM_Billing_Method_ID
           ELSE 'NONE' 
      END AS BillingMethod, v_rpt_Invoices.Invoice_Amount, 
      CASE WHEN (TicketNbr <> 0 AND SR_Service.Bill_Method ='A') THEN Invoice_Amount * 0.7 
           WHEN (TicketNbr <> 0 AND SR_Service.Bill_Method ='F') THEN 0.01 
           WHEN (project_id IS NOT NULL AND PM_Project.PM_Billing_Method_ID = 'A') THEN Invoice_Amount * 0.7 
           WHEN (project_id IS NOT NULL AND PM_Project.PM_Billing_Method_ID = 'F') THEN 0.01 
           ELSE 0.00 
      END AS Cost
FROM v_rpt_Invoices 
INNER JOIN v_rpt_Company ON v_rpt_Invoices.Company_RecID = v_rpt_Company.Company_RecID
LEFT JOIN   SR_Service ON v_rpt_Invoices.TicketNbr = SR_Service.SR_Service_RecID
LEFT JOIN   PM_Project ON v_rpt_Invoices.PM_Project_RecID = PM_Project.PM_Project_RecID
LEFT JOIN  v_rpt_Service ON  v_rpt_Invoices.TicketNbr = v_rpt_Service.TicketNbr
WHERE     (v_rpt_Invoices.Date_Invoice >= '1/1/2013')
order by Territory, Invoice_For