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 のサイズです。

これは、最適化できる潜在的なサブクエリの 1 つにすぎません。

サブクエリを例にローリングする例を次に示します(テストできないため、バグ/タイプミスがある可能性があります)

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 に答える