• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

Incorrect syntax near '?'.

Dear experts
 I am using this sql (our database is Ms SQL Server 2008 and we have defined our sql in a spring xm file)
 
select distinct cg.NAME as CustomerGroup,COUNT(cg.NAME) as GroupCount		 
		 FROM PAC.CustomerGroupMap cgmap,
		 PAC.CustomerGroup cg,			
		 PAC.Customer Customer,
		 PAC.Customercluster customerRef
		 where cg.CustomerGROUPID = cgmap.CustomerGROUPID and
		 cgmap.CustomerID = Customer.CustomerID 
		 and customerRef.CustomerGROUPID = cgmap.CustomerGROUPID
		 and (customerRef.MODIFIEDBY != 'Migration' OR customerRef.ISACTIVE != 'F')
		 and customerRef.CLUSTERID = ?
		group by cg.NAME		
		having COUNT(cg.NAME) = 1

Open in new window

           
            But it gives this exception
                  ]; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '?'.
                  
                  the parameter passed is a number like 100, 200, ect.
                  Any clue where the problem is.
                  
                  Thanks.
0
royjayd
Asked:
royjayd
2 Solutions
 
Robberbaron (robr)Commented:
the parameter is usually something like @param1

unless you are calling using OLEDB ?  see https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters(v=vs.110).aspx

if not, then the parameters should be named.
0
 
PortletPaulfreelancerCommented:
this is not about the question, but about the query.

1. Do not use select distinct & group by


There is NEVER a reason to use BOTH "select distinct" in a single query containing a "group by" clause.

The group by clause will always determine the distinct set of data that matches the fields specified. It is simply not useful to ask for select distinct of something that is already distinct.

nb: despite the way sql is written, the select clause is performed AFTER the group by is completed.

2. use ANSI join syntax


ANSI standards for joins are over 20 years old already, time to make the change.

e.g.
SELECT
  cg.NAME AS CustomerGroup
, COUNT(cg.NAME) AS GroupCount
FROM PAC.CustomerGroupMap cgmap
INNER JOIN PAC.CustomerGroup cg ON cgmap.CustomerGROUPID = cg.CustomerGROUPID
INNER JOIN PAC.Customer Customer ON cgmap.CustomerID = Customer.CustomerID
INNER JOIN PAC.Customercluster customerRef ON cgmap.CustomerGROUPID = customerRef.CustomerGROUPID
WHERE (customerRef.MODIFIEDBY != 'Migration'
OR customerRef.ISACTIVE != 'F')
AND customerRef.CLUSTERID = ?
GROUP BY cg.NAME
HAVING COUNT(cg.NAME) = 1

Open in new window

0
 
royjaydAuthor Commented:
right on. thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now