I am confused as to why it would bring back the syntax error if I
select more than one from list.
Short answer
Because WHERE LOAD_NO IN (CAST(1,2,N as DECIMAL))
is not a valid T-SQL statement.
Long answer
When you use a multi-value parameter in a query, reporting services will generate different queries if your parameter contains 1 value, or multiple values.
Let's simplify your example to the following query:
SELECT * FROM TABLE WHERE LOAD_NO IN (@Load)
With only one value, the query will have the following format:
exec sp_executesql N'SELECT * FROM TABLE WHERE LOAD_NO IN (@Load)', N'@Load int', @Load=<YourValue>
It's a query with a parameter: @Load
.
Now, with multiple values, the query will become
exec sp_executesql N'SELECT * FROM TABLE WHERE LOAD_NO IN (<YourValue1>, <YourValue2>,<YourValueN>)'
The @Load
parameter has been replaced by the list of values.
So now my advise will be to rethink the design of your query and treat @Load
as a list of values.
We cannot provide you the best solution because it really depends on the data and only you have all the details but I could still throw some ideas.
On the top of my head I could think of:
- Cast
LOAD_NO
instead, but the execution plan may loose the benefits of indexes if any.
- In most cases, using a
IF EXISTS
when possible instead of IN
.
- Use a subquery.
Do not hesitate to run a SQL Server Profiler to see the generated query if you have other issues.