1

场景:我正在 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
4

1 回答 1

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
于 2013-01-31T15:28:25.470 回答