Creating a concatenated string in SSRS with values enclosed in single quotes

纵然是瞬间 提交于 2019-12-13 03:42:04

问题


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 joined 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!