How to handle a parameter that has no value in SSRS - reporting-services

I have a report that has two datasets in it. The first dataset is used to populate a parameter to a second dataset. Everything works fine until the first dataset has no values, whereupon I get a parameter X is missing a value message.
Is there a way to get around this?

Assuming you're using Cascading Parameters, you can use a query similar to the following to populate the second parameter's DataSet:
if exists (select 1 from Param2 where paramVal = #Param1)
begin
select paramVal, paramDetail
from Param2
where paramVal = #Param1
end
else
begin
select paramVal = 'None', paramDetail = 'None'
end
This is just a workaround using an if...else construct to return a dummy row if there aren't any other rows to return - that way you'll always get at least one row returned even if there are no matches for the first parameter value.
You can handle these dummy rows as required later in the report.
As above this is a workaround to address your specific issue, but hopefully will be of some help.
Added after comment:
I just tested now - it works just the same with multi-valued parameters; just change =#Param1 to in (#Param1).
If there are one or more matches in the second DataSet it returns those rows, otherwise just the dummy row.
I did need to shut/reopen the report in VS, but that could have been unrelated.

Related

What is the SSRS Multi Value Data Type and how to use

I have a multi-select.
I think the underlying datatype is int || array(int). This is pretty frustrating that you have to do a check to see if a multi-value is present before jumping into an index. But how does this value get passed to SQL?
It's easy enough to use in a IN (#variable) statement. How else can it be used? Is it a string or a table. From my investigations it appears to be single table row with many un-named columns but I'm not really sure.
Finally, when you want to simulate a multi-select in a query inside visual studio, for example to "Refresh Fields" how do you do that? For example "1,2,3", {1,2,3} or #{1,2,3}. It's not (123) because that is -123.
It dpends what you are trying to do and in what context.
As you said, if you have a datset query that is a SQL script (as opposed to a stored proc) then you can use IN(#paramName). In this instance SSRS take the parameter values (not the labels) and injects them into the sql statement as a string e.g. '1,2,3'. The result would be IN(1,2,3). If you want to pass in a list of, say, countries then you would have to set the parameter values to be the same as the parameter labels So rather then Value =1, Label = Spain you would have Value = Spain and Label = Spain. Used in an IN() would generate something like IN('Spain', 'France').
If you try to do the same with a stored proc e.g. EXEC myProc #myParam, then the parameter values would be passed as a sing string which would then need to be split out by the proc.
If you just want to get a list of selected parmeter values or label shoing in your report then you can simply do something like
=JOIN(Parameters!myParam.Value, ",")
or
=JOIN(Parameters!myParam.Label, ",")
where "," is the delimiter
If you pop this expression in a text box, you'll get a list of the selected parmater values/labels
I think it's a kind of madness but I found a workaround to get a table of values from the results from SSRS. I query the IDs against a source table using IN(). I hope there is a better way of doing this?
SELECT [TblFeeBillingCycleID]
FROM [TblFeeBillingCycle]
WHERE [TblFeeBillingCycleID] IN(#intCycleId)

SSRS Multi-Value Parameter - When Select ALL, returns zero rows

My current definitions for this report are like this:
DataSet A = a pretty extensive query with 'AND ai.Channel IN (#ChannelParameter)'
DataSet B = Channel 'Select distinct channel from Account_Info'
result set 'Retail Channel' or 'Wholesale'
Parameter = #ChannelParameter is set to Allow Multiple Values and is Getting the values from Query.
Tablix Properties - Filters
Expression [Channel]
Operator IN
Value =Parameters!ChannelParameter.Value(0)
When I run the report and select 'Retail Channel', I get the correct data.
When I run the report and select 'Wholesale', I get the correct data.
When I run the report and select both values, I get zero rows returned.
When I modify the query for DataSet A to be ai.Channel IN ('Retail Channel','Wholesale'), I get all of the rows. There are no rows in the data where the Channel field is NULL.
I've seen and tried some changes to the expression in the parameter using a JOIN statement, but no better results there.
What am I missing?
You filter expression is incorrect, as you stated (0) that means only the first parameter value will be used.
Having said that, it looks like you are already filtering the data in your A dataset so there is no need for the tablix filter, just remove it.

Using SSRS report to pass multiple string parameter with single quotes

I am failing to pass parameter value to data set using ,
=split(Parameters!Status.Value,",")
Parameter (#status)defult values -> C,P
Returns Error:
The value expression for the quer parameter '#status' contins ans error: Conversion from type 'Object()'to type 'String' is not valid.
As it looks that the parameter is already a string, what you need to convert it into list is a join function:
=join(Parameters!Status.Value,",")
once you pass the comma separated list to SQL you may need to search the list as discussed here:
SSRS selecting results based on comma delimited list
You could do this the way you want but I would approach this differently as it's easier to see what's going on especially if this gets passed to somebody else to debug later.
There are two ways of doing this. The first is to pass in a single value for Close or Open and then add some logic to the dataset
DECLARE #s TABLE(status char(10))
IF #states = 0 -- Closed
BEGIN
INSERT INTO #s VALUES('c'),('p')
END
ELSE
BEGIN
INSERT INTO #s VALUES('o')
END
select * from myTable a
JOIN #s s on a.status = s.status
I realise it's more code but it's simple to follow.
IF THIS WORKS FOR YOU STOP READING NOW !!
The second option (assuming you don't want your users to just be able to select 'c' or 'p' individually) it a bit more effort to set up only the SSRS side but the SQL is very simple. Don't be put off by the length of the answer it actually pretty simple.
Your dataset query would just look something like this...
select * from myTable a WHERE a.Status in (#status)
In your report design set the #states parameter available values to c, p and o as separate items and make the parameter multi-value.
At this point you should be able to test that the report works and you can select 1, 2 or all three options and get the relevant results, there is no need for joins splits or anything else, it will just work.
Now, as I assume you don't want your users to be able to choose at this level, we need to make some changes.
First add a new dataset called say dsDefaultStatus and set the query to something like
DECLARE #s TABLE(status char(10))
IF #status = 0 -- Closed
BEGIN
INSERT INTO #s VALUES('c'),('p')
END
ELSE
BEGIN
INSERT INTO #s VALUES('o')
END
SELECT * FROM #s
You should new have a new #status parameter. Give this two available labels/values such as 'Close'=0 and 'Open'=1 to match the query above.
Now go back to the #states parameter and change the default values to dsDefaultStatus, you can then hide this parameter if required.
As the user selects the Open/Close status in the first parameter, the defaults selection for this hidden parameter will change. When you run the report the values from the hidden parameter get passed to the report.

SSRS expression with missing column not working

I'm trying to create an expression with a missing column. as in the column is not returned from the query but does exist in the list of fields.
The problem I'm having is that every time I do an expression and one of the parameters is from a field that doesn't have a return from query the query fails silently. the following example is looking at a field "test" that, as I said, exists in the list of fields but is not returned from the query, how can I have this is statement send "alert"??
=IIF(Fields!test.IsMissing,"alert",Fields!test.Value)
The reason that I don't return the field is that the columns are dependent on the parameters I enter in the procedure (so they could be used or not depending on what the user is asking)
Thank you
From reading your question I think, you have dynamic columns which will be returns conditionally
So what you should do is,
1) Create the parameter in the parameter list and set it as the internal and assign the field value to that parameter, suppose the parameter is dyanmicfiledvalue
2) Change your expression as,
=IIF(IsNothing(Parameters!dyanmicfiledvalue.value),"alert", Parameters!dyanmicfiledvalue.value )
that should do it. Let me know in case of issues.
Or if you want to check null or empty value for that column just change as
=IIF(IsNothing(Fields!test.value),"alert",Fields!test.value)

Fields cannot be used in report parameter expression

I have to set the start_date of my report depending of a report parameter. The time stamps are calculated in a database query.
My expression looks like this:
=SWITCH (
Parameters!report_type.Value = 1,First(Fields!daily_start.Value, "Timestamps")
,Parameters!report_type.Value = 2,First(Fields!weekly_start.Value, "Timestamps")
,Parameters!report_type.Value = 3,First(Fields!monthly_start.Value, "Timestamps")
)
Unfortunately I get the error message:
A value expression used for the report parameter 'time_from' refers to a field. Fields cannot be used in report parameter expression
I know, that this is not allowed because SSRS cannot be sure in which order datasets are called. But I think this is not dangerous.
All time stamps are received by query without parameter. The parameter report_type is selected by a user before the report will be generated.
Can someone give me a hint for a workaround?
Here's the workaround - get the value using SQL.
Create a new Dataset called StartDates:
SELECT CASE
WHEN #report_type = 1 THEN daily_start
WHEN #report_type = 2 THEN weekly_start
WHEN #report_type = 3 THEN monthly_start
END AS StartDate
FROM MyTable
You already have the #report_type and #time_from parameters. With the #time_from parameter, set its Default Values to Get values from a query using the StartDates dataset and the Value field StartDate.
Now, you'd think this might be enough to make it work - you're referencing this query as the default value and as you change the #report_type parameter the other parameters refresh, but the first date in the #time_from parameter never changes. That's because the refresh happens on the Available Values query, not on the Default Values query.
So you also need to wire up the Available Values query to the StartDates query. Now your query will fire on the change of #report_type and the default value will be set to the appropriate date for your selection.
I switched from a query to Stored Procedure and was getting this error. Things I tried:
Ensured I had sufficient permission on the database (you need EXEC rights or DBO to run teh sproc)
Delete the existing parameters (and then use refresh fields to refresh/get the correctly named ones back)
Remove the square brackets around the stored procedure if you've specified that
Sometimes, Expressions can get a bit verbose. I have created a Report Code Function and then used that as the Parameter Value.
For example, I created a Code function called "CalculateDateSet" and then set the Report Parameter to this expression:
"=Code.CalculateDateSet(Parameters!Month.Value, Parameters!Year.Value"