问题
I'm just starting developing reports in SSRS and would appreciate some help with this issue if possible! I'm selecting a dataset from a Dynamics database and want to then pass them to a SQL Server stored procedure referenced in another dataset to retrieve data from another database. I have created a report parameter and set it to Allow multiple values and to retrieve its values from a query and set it to the field that I want to retrieve.
The dataset would look like this:
U1234
U5678
U6789
In the dataset that uses the stored procedure I have set up a parameter, @pnum
, and in the Parameter Value field I have created an expression using the Join
statement like this:
Join(Parameters!pnum.Value, ", ")
When this gets passed to the stored proc it seems to be passing a string formatted like this:
'U1234, U5678, U6789'
Whereas what I would like to achieve is this:
'U1234', 'U5678', 'U6789'
so that I can use them in an IN
statement. Is there a way of doing this within SSRS?
Many Thanks!
回答1:
To anyone else experiencing this issue, the assumption made in the question on how the values are passed to the stored procedure and how they can be used are incorrect.
The value passed from the join
expression would be formatted as such, without single quotes at the start and end:
U1234, U5678, U6789
Further to this, when passed to a stored procedure as a single string this can only be used as an in
list by using dynamic SQL.
To parse out and filter on the passed values, the string will need to be split on the delimiter and inserted into a table (temporary or otherwise) to be join
ed to.
A suitable splitting can be found here (though others exist that may better suit your needs) utilising logic as follows:
declare @xml as xml,@str as varchar(100),@delimiter as varchar(10)
set @str='A,B,C,D,E'
set @delimiter =','
set @xml = cast(('<X>'+replace(@str,@delimiter ,'</X><X>')+'</X>') as xml)
select N.value('.', 'varchar(10)') as value from @xml.nodes('X') as T(N)
If you don't have to pass the values to a stored procedure and are using hardcoded datasets (Shared or not) you can actually directly use the parameter value without additional processing either in the query or by adding a join
expression to the parameter value in the report:
select cols
from tables
where cols in(@MultiValueParameterName)
回答2:
You have to add an extra field with the value wrapped in quotes.
Like this:
SELECT field1 AS display, '''' + field1 + '''' AS value
来源:https://stackoverflow.com/questions/27247552/creating-a-concatenated-string-in-ssrs-with-values-enclosed-in-single-quotes