Symptom
- Error: Incorrect syntax near ','
- When entering multiple values in a parameter, the report generates an error.
- When entering only one value in a multiple values parameter, the report refresh successfully.
- When refreshing a report based on a Command Object to MS SQL Server, and entering multiple values, Crystal Reports generate error like:
"Failed to retieve data from the database.
Database Connector Error: '42000:[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]
Incorrect syntax near ','. [Database Vendor Code: 102]' "
Environment
- SAP Crystal Reports 2013
- SAP Crystal Reports 2016
- SAP Crystal Reports 2020
- MS SQL Server 2014
- MS SQL Server 2016
- MS SQL Server 2017
- MS SQL Server 2019
Reproducing the Issue
- In Crystal Reports, create a report based on a MS SQL Server database using an ODBC connection.
- In a Command Object, enter a custom SQL Query.
- Still in the Command Object, create a parameter, and check the option "Allow multiple values"
- In the customer SQL Query enter in the WHERE clause of the SQL Query the parameter surrounded by parenthesis, like:
SELECT Country, CustomerName
FROM Customer
WHERE Country IN ({?MyParameter})
- When refreshing the report, and entering multiple values in the parameter, it fails with the error.
Cause
- When setting a parameter to multiple values in a Command Object, it substitute the parameter in the custom SQL Query, by a string with the values entered in the parameter, surrounded by parenthesis like:
('France', 'Germany')
- Since the parameter is already surrounded by parenthesis in the custom SQL Query written, like:
WHERE Country IN ({?MyParameter})
Then the SQL Query sent to the database will be like:
WHERE Country IN (('France', 'Germany'))
And the double parenthesis is not a valid syntax for MS SQL Server, and causes the error when entering multiple values in the parameter.
Resolution
- Remove the parenthesis around the parameter in the custom SQL Query of the Command Object for the report in Crystal Reports:
- In Crystal Reports, open the report.
- Under the menu "Database", select "Database Expert"
- In "Database Expert", under "Selected Tables", right click on the Command object, and select "Edit Command"
- If prompted, logon to your database.
- In the window "Modify Command", edit the SQL Query, and remove the parenthesis around the parameter set to use multiple values.
- Click "OK" to accept the change, and save your report.
Keywords
CR , KBA , BI-RA-CR , Crystal Reports designer or Business View Manager , Problem
Product
SAP Crystal Reports 2013 ; SAP Crystal Reports 2016 ; SAP Crystal Reports 2020