How can I include multi-select parameters in my SSRS report? - sql

I have an SSRS report with a multi-select parameter.
I want to include the parameter in my data-set. The data-set is made using a SQL query. Usually, I would do something like this:
SELECT * FROM table WHERE value = #parameter
How would I achieve the exact same outcome, using a multi select parameter?
E.G. the parameter could have the following options ticked:
Option 1
Option 2
Option 3
So my query should work like this:
SELECT * FROM table WHERE value in ('Option 1', 'Option 2' 'Option 3')
So, with a parameter, i'd imaging it would look like this:
SELECT * FROM table WHERE value in (#parameter)
But what would the correct syntax be to refer to the multi-select report parameter using my dataset built from an SQL query?

If you query is directly in the report's dataset (so NOT a stored procedure) then your final SELECT would work
SELECT * FROM table WHERE value in (#parameter)
SSRS will convert the values into a comma separated list and inject them into your script so you don't need to do anything.

on SSRS 2016 I can use
= any(string_to_array(#parameter, ','))
also make sure to allow for multiple values

Related

SSRS Dropdown for column items

I've built a SSRS report using a SQL query for the Dataset1. I'm trying to build a parameter that gives the users a dropdown list. I ended up creating a Dataset2 to get distinct values for the parameter Label and then use Dataset1 for the actual value (allowing for multiple values).
EDITING PER Request:
Dataset1 query:
SELECT vu_SOPWork_HistoryUnion.Type
,vu_SOPWork_HistoryUnion.SOPTYPE
,vu_SOPWork_HistoryUnion.SOPNUMBE
,vu_SOPWork_HistoryUnion.Date_Document
,vu_SOPWork_HistoryUnion.ExtendedPrice
,IV00101.ITEMNMBR
,IV00101.USCATVLS_2 AS Family
,IV00101.USCATVLS_3 AS Product
,vu_SOPWork_HistoryUnion.VoidStatus
,RM00101.CUSTCLAS
,GL00100.MNACSGMT
,vu_SOPWork_HistoryUnion.BillTo_CustNum
,vu_SOPWork_HistoryUnion.BillTo_CustName
,vu_SOPWork_HistoryUnion.sales_territory
,vu_SOPWork_HistoryUnion.ITEMDESC
FROM (
(
test.dbo.vu_SOPWork_HistoryUnion vu_SOPWork_HistoryUnion INNER JOIN test.dbo.IV00101 IV00101 ON vu_SOPWork_HistoryUnion.ITEMNMBR = IV00101.ITEMNMBR
) INNER JOIN test.dbo.RM00101 RM00101 ON vu_SOPWork_HistoryUnion.BillTo_CustNum = RM00101.CUSTNMBR
)
INNER JOIN test.dbo.GL00100 GL00100 ON IV00101.IVSLSIDX = GL00100.ACTINDX
Dataset2 Query:
SELECT DISTINCT IV00101.USCATVLS_2 AS FamilyNames
FROM test.dbo.IV00101
I would like to use the Dataset2 "FamilyNames" as the label options against Dataset1 "Family" values. Dataset2 gets the distinct values that I want to put in the drop down for the user to choose and then receive all rows that has that value in Dataset1 "Family". I'm an expert on not giving enough information. Hope this helps you help me. Thanks.
OK, do the following..
Create DataSet1 with the query as you have it now but append the following to the end of the query
WHERE USCATVLS_2 IN(#Family)
When you do this, the #Family parameter will be created automatically in your report, we'll get back to this later.
Create another dataset called Dataset2 (or a more sensible name like 'families' in this case) with the following simple query.
SELECT DISTINCT USCATVLS_2 FROM test.dbo.IV00101 ORDER BY USCATVLS_2
Edit the #Family parameter:
Set it to multi-value
Change the available values to be a query
Select your 2nd dataset as the datasource
Choose USCATVLS_2 as both the labels and values.
Create your report as normal with the tablix/matrix based on Dataset1.
Notes:
A few other things you might want to consider, but not required.
If a list of family names with some kind of ID is available in your database then I suggest you use that for your parameter list, you would also then have to edit the query in dataset1 to match. Remember that the parameter will contain whatever is in the column of the query that you chose as the value column in the dataset that populates the parameter list
Consider using aliases for table names in your queries rather than the full table names all the time, it makes the code more concise and easier to read.
Name your datasets according to what they contain or do, e.g. I would call Dataset2 'Families' or something similar as that is what it contains. When your reports get more complex it will make them easier to understand.

How to manipulate multi-value string parameters for a SQL Command in Crystal Reports

I have a Crystal Report based on a SQL Command that, in part, consists of procedure names. What I'm trying to do is add a multi-value string parameter to the SQL Command such that the end users can enter partial procedure names and the report will return only those relevant procedures that string match.
For example, a user should be able to enter "%KNEE%" and "%HIP%" into the parameter and return all procedures that contain the words "KNEE" and "HIP". The problem is that I can't figure out how to manipulate the parameter value in the SQL to accomplish this. I've done this before with a report parameter (as opposed to a SQL Command parameter) by simply adding the line {table.procedure_name} like {?name match parameter} to the record selection formula, but taking the same approach in the SQL Command gets me an "ORA-00907: Missing right parenthesis" error.
Any suggestions on how I can manipulate the multi-value string parameter to accomplish this?
I dont like to post this as an answer because I don't care for the solution however it is the only way I have found to work around this.
I have had to instruct users to enter '%KNEE%','%HIP%','%ETC%' at the parameter prompt. Then the {table.procedure_name} like {?name match parameter} should work in your SQL. Not optimal, especially for your scenario with the %. I would love to hear someone provide a better solution because I have wrestled with this many times.
Here's an approach:
SELECT column0
FROM table0
INNER JOIN (
SELECT trim('%' || x.column_value.extract('e/text()') || '%') SEARCH
FROM ( SELECT 'arm,knee' options FROM dual ) t,
TABLE (xmlsequence(xmltype('<e><e>' || replace(t.options,',','</e><e>')|| '</e></e>').extract('e/e'))) x
) v ON column0 LIKE v.search
Use Oracle's XML functionality to convert a comma-delimited string to an equivalent number of rows, wrapping each clause with %%. Then join those rows to the desired table.
To use with CR, create a single-value, string parameter and add it to the code:
...
FROM ( SELECT '{?search_param}' options FROM dual ) t,
...

SSRS: When Multi-value Parameter = Select ALL remove filter in Script

I have a report that has multiple multi-value parameter. What I wanted to do is if the parameter is = Select All I'll remove that parameter to my SQL Script.
Example is I have a Product group and Product Name parameter and what I want is if the user selects all the product group my script will be like:
SELECT * FROM TABLE WHERE PRODUCT_NAME IN (#ProductName)
While if the user did not select all Product Group, my script will be like:
SELECT * FROM TABLE WHERE PRODUCT_NAME IN (#ProductName)
AND PRODUCT_GROUP IN (#ProductGroup)
I want to know how Can I detect when multi-value parameter is = Select All. I think it will really help the loading time of the tool if I just remove the filter on my script.
As far as I know, you cannot detect when "Select All" has been checked by the user.
To remove the filter completely when the user selects all the choices in a multi-valued parameter you would have to employ logic in your stored procedure that checks to see if all the choices were passed to it in the parameter, and if so, don't use the parameter at all in the main query.
You can add your own ALL value to the parameter. In the dataset query check if the user have selected 'ALL' if so don't use the parameter.
Something like this:
IF ('ALL' IN #ProductGroup)
BEGIN
SELECT * FROM TABLE WHERE PRODUCT_NAME IN (#ProductName)
END
ELSE
SELECT * FROM TABLE WHERE PRODUCT_NAME IN (#ProductName)
AND PRODUCT_GROUP IN (#ProductGroup)
It is not tested but should work
If your parameter is based on a dataset, you can compare the numbers of elements selected in the parameter vs. the number of items in the dataset for the parameter.
=IIF(Parameters!AREA.Count = CountRows("Areas"), "ALL", "Some")

Multi-value parameter used with execute(#sql)

I'm having a hard time trying to figure out how to make this work :
I have a big SQL query used for a report, that is run using "execute(#sql)".
Withing #sql I have various select statements that I would like to filter using the values in the multi-value parameter passed from my report #filtervalues.
I've tested the following (this is an example query):
Set #Sql='Select * from my table where field in ('+#filtervalues+')'
When I select a single value in the parameter, the profiler reads it as follows:
Select * from mytable where field in(1234356-1234-1234-1234-123456)
So the selected Guid is without quotes, resulting in "error near 123456"
On the other hand, if I selected several values:
Select * from mytable where field in ('+N'1234456-1231-1234-1244566',N'2345333-3243-2342-2342-23423423'+)
So it adds extra ' and +
Can you help me with this ?
Note:
I cannot use the suggested solution on many websites to apply the filter on the table directly, because this would mess up the data.
Edit:
The multi-value parameter is filled using this query dataset:
select 'No Filter' as fullname,'00000000-0000-0000-0000-000000000000' as systemuserid
union all
select distinct su.fullname, convert(nvarchar(90),su.systemuserid)
from FilteredSystemUser su
I managed to fix this using the following :
1- In my Dataset, instead of passing the parameter as it is, I changed it to :
Join (Parameters!FilterValues.Value," ',' ")
to force the values to be sent as strings, the parameter value is as follows :
1234567','122345','12345
2- In SQL, in my procedure, I added additional ' at the beginning and the end of the parameter to make up for the missing ones. So my code shows the following:
Set #Sql='Select * from my table where field in ('''+#filtervalues+''')'

Array parameter for TADOQuery in Delphi 2010

I need to execute a simple query:
SELECT * FROM MyTable WHERE Id IN (:ids)
Obviously, it returns the set of records which have their primary key 'Id' in the given list. How can I pass an array of integer IDs into ADOQuery.Parameters for parameter 'ids'? I have tried VarArray - it does not work. Parameter 'ids' has FieldType = ftInteger by default, if it matters.
There is no parameter type that can be used to pass a list of values to in. Unfortunately, this is one of the shortcomings of parameterized SQL.
You'll have to build the query from code to either generate the list of values, or generate a list of parameters which can then be filled from code. That's because you can pass each value as a different parameter, like this:
SELECT * FROM MyTable WHERE Id IN (:id1, :id2, :id3)
But since the list will probably have a variable size, you'll have to alter the SQL to add parameters. In that case it is just as easy to generate the list of values, although parametereized queries may be cached better, depending on which DB you use.
The IN param just takes a comma separated string of values like (1,2,3,4,5) so I assume you set the datatype to ftstring and just build the string and pass that...? Not tried it but it's what I would try...