Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9239235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:56:27+00:00 2026-06-18T07:56:27+00:00

Scenario: I am building a report in Excel to calculate commissions. This is based

  • 0

Scenario: I am building a report in Excel to calculate commissions. This is based on Invoices from the previous month.
I created the following tsql query and created a connection using MSQuery. I tested the query and it works perfect until I change my Where statement to use the parameter "?" so I can , then I get the following error:

Incorrect Syntax near the keyword `'SELECT'`

Here is the query:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:56:28+00:00Added an answer on June 18, 2026 at 7:56 am

    Remove this line

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

    That is not valid SQL — looks like you want to comment use double dash.

    Unless you are just putting the ** to show what you changed. In that case you need to have a value where the ? is the SQL won’t work.


    As an asside, this query could be made much clearer and faster. Consider consolidating sub-queries into a join. For example, the following sub-query

    SELECT     Bill_Method
    FROM          SR_Service
    WHERE      v_rpt_Invoices.TicketNbr = SR_Service.SR_Service_RecID
    

    MUST be called for every row — if you make this a join you go from O(nm) to O(n+m) Where n is the size of v_rpt_Invoices and m is the size of SR_Service.

    This is just one of the potential sub-queries you can optimize.

    Here is an example of rolling in the sub-queries to your example
    (I can’t test so it might have bugs / typos)

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a WCF router and my client uses Reliable Sessions. In this scenario
I am currently building something in JS and have run into the following scenario:
I'm building a report where I have a subreport. This subreport brings some data
scenario: I have difficulty in building a single query that would display a report
We're building an application which uses ACS. Our usage scenario looks like this: The
i have trouble with building some projects. please consider this scenario: i have 2
I am building a blog engine using MVC 3 and razor. In this scenario,
Client is building a web based Digital Asset Management system. Here is the scenario:
I am building a mysql based chat application. My database schema has the following
please help me with this : The scenario: building a system (Backend :Mysql ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.