简体   繁体   中英

Using SSRS report to pass in a param value the is a csv list to SQL Dataset

I've made an SSRS report which has a text field parameter where users will enter a comma separated list of string values. That list will be fed into a SQL query in the report's Dataset which has a: ...WHERE TheField in (@myCSVParam).

If I pass in a single value from the field, it works, but if I pass a csv list from the field then the report says that such value doesn't exist.

I think it is treating the csv list as a single value and hence no data comes back.

How do I convert that csv list and convert to a usable param for a sql IN filter?

I was searching for a way to do this today, and there are a lot of suggestions which require building a Stored Procedure to handle this, but I found this solution to be a bit of a mess. I finally came up with my own, really simple solution.

So, lets say you want the user to be able to enter multiple sales orders in a single text box, separated by commas, eg: 61JF073, 61SW073, 61SW074

My first report parameter is named "OrderNo", description "Order Nos (Separated by comma)". Data type="Text", and nothing is checked.

My second parameter is going to take the list from the first parameter and make a "Multiple Values" list which you will be able to use in your SQL query.

Second Parameter: Name = "OrderNos", type = "Text". "Allow Multiple Values" is Checked. Parameter visibility = "Internal" (the user does not need to see this). Available Values = None Default Values = Specify Values Add a value of: =Split(Replace(Parameters!OrderNo.Value," ",""),",")

I am using a "Replace" function to remove all the spaces as users may enter spaces after the commas which will affect the output.

Now your SQL query can contain your "IN" clause to filter the results.

SELECT OrderNo, SalesID, SalesName FROM SalesOrders WHERE OrderNo IN (@OrderNos)

The data will be filtered at the SQL server instead of in the SSRS report which is the preferred method to save the SSRS report from retrieving the entire table before applying a filter. You only want to retrieve the data required for reporting.

I hope this helps as it was a lifesaver for me as my sales order table has many thousands of rows.

instead of 2 parameters, I used something like =split( Parameters!Category.Value ,",")

and set the parameter used in the SQL [In (@Category) ] set to that expression. The only downside is it's buried in the dataset parameters tab.

I eventually changed it to =split(replace(Parameters!Category.Value," ",""),",") like yours :) Up to you, if you would rather have 1 parameter and have the parameter equal to an expression, or have 2 parameters, and pass 1 through the other.

I can't post images but mine is here http://i.stack.imgur.com/rhnWt.png

It sounds like @myCSVParam is a single-value string parameter. Accordingly, SSRS will put quotes around it when using it in a SQL statement. So for one value you are getting 'First' (which is a match) but for multiple values you are getting 'First, Second, Third' (that is, one large value that doesn't match anything) rather than 'First', 'Second', 'Third' (that is, a list of three separate values) which is what you are after.

You have two options:

  • Use a multiple value parameter, which naturally does what you what. This is the best option if the user can select values from a list
  • Manually create the list by replacing the commas with quoted commas, like so:

    =Replace(Parameters!myCSVParam.Value, ", ", "', '")

You can try as the following query:

WHERE TheField in (SELECT regexp_substr(@myCSVParamt,'[^,]+', 1, level) A FROM DUAL CONNECT BY regexp_substr(@myCSVParam, '[^,]+', 1, level) is not null)

Hope it help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM