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.
royjaydAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
PortletPaulEE Topic AdvisorCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
royjaydAuthor Commented:
right on. thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.