SSRS: Passing/Setting parameter to Dataset using Expression - sql-server

I am using Microsoft SQL Server Report Builder 3.0. I have created a Stored Procedure (stored_procedure1) in the database which has a Parameter (parameter1). Here, stored_procedure1 returns result1.
Then, I used stored_procedure1 to create a Dataset (dataset1) in the Microsoft SQL Report Builder 3.0. Next, I created a Table (table1) in Microsoft SQL Report Builder 3.0 with 2 rows and 2 columns (total 4 cells).
I would like to fill each element of table1 with result1 from dataset1. Hence, I set expression of each cell of table1 as follows:
=Sum(Fields!result1.Value, "dataset1")
When I run this report, it works perfectly and asks me to enter parameter1. However, I want to use single Dataset (dataset1) with different values of parameter1 for each cell of the table. Hence, I want to pass/set parameter1 with unique parameter_value for each expression of table cells. Say I want to set parameter1 = parameter_value1 for first cell.
For example, if I need to set parameter_value = 5, I did something like
=Sum(Fields!result1.Value, "dataset1"), Parameters!parameter1.Value = 5
I also tried following:
=Sum(Fields!result1.Value, "dataset1") & Parameters!parameter1.Value = 5.
It doesn't work.
In summary, I coudln't pass or set parameter value together with an expression.
Can we set/parameter value.
I would like to thank you in advance.

If anyone got stuck with this problem, I found a way around for this. T
here is no way one can pass parameter within expression.
You will need to create a subreport to do this. You can pass parameter to subreport.
Its little time consuming. However, it seems there is no other way around.

Related

How to display a multi-valued parameter onto a SSRS report in a specific way, separated in ranges and/or commas

In SSRS I need to display a multi-valued parameter onto the report in such a way that if values are chosen in sequence they appear as: 1-5, 7, 9-10, 15 and so on. And I have the following values in my drop down list of values: from '0' to '200'.
Thanks in advance for your help.
This is what I have done in SQL so far, so I am thinking to update 'String_To_Use' column so it would display: 0000-1020, 1199-1210, 1260, 1299. Then use this string to display onto the SSRS report. These are the values chosen in SSRS from the drop down box. I don't know yet how I would pass these values to the SQL code yet. Please help with this part as well.
This is the #tempTable1...column 'DPRTMNT' has the values chosen ...Checking_Dept has the value-2 when values in ranges
This is the #tempTable2..I need to update 'String_To_Use' column so it would contain: 0000-1020, 1199-1210, 1260, 1299. #tempTable1 can help to build the logic
replace dsBranchPlant with name of your data set that your using to supply data parameter. And "All" condition is if all params are choosen instead of showing each one. Game this up as an expression and see where you stand.
iif(Parameters!BranchPlant.Count = CountRows("dsBranchPlant"),"ALL",Join(Parameters!BranchPlant.Label,","))

Multi value parameter not working in SSRS report

I have a SSRS report. there is a long SQL query on the main query, in the last SELECT I want to filter the results with WHERE expression, the filter should be with a multi value parameter.
I set the parameter in this way:
Create a new Dataset with a query.
Add a new parameter to the Parameters folder (with name NewParam).
Check the "Allow multiple values" checkbox.
Add the parameter to the "Main Query" and set the value with this expression:
=Join(Parameters!NewParam.Value,",")
At the end of the Main Query I filter the results:
select *
from #FinalStatusTbl
where Test_Number in (#NewParam)
order by Priority
The problem is:
On the report when I choose one value from the list I got expected results, but If I choose multi values the results are empty (not got an error.)
Do you have any idea why?
(When I try this: where Test_Number in ('Test 1', 'Test 2') it works well).
When you create a dataset with a sql query, multi valued parameters work with the in(#ParamName) without any changes.
Replace your =Join(Parameters!NewParam.Value,",") with just =Parameters!NewParam.Value and you should be fine.
That said, the reason you see people using that join expression is because sometimes your query will slow down considerably if your parameter has a lot of potential selections and you data is reasonably large. What is done here is to combine the join expression with a string splitting function in the dataset that converts the resulting Value1,Value2,Value3 string value in a table that can be used in the query via inner join.
This is also a requirement if passing multiple values as a parameter to a stored procedure, as you can't use the in(#ParamName) syntax.
You could try taking the parameter out of the where clause and use the parameter in the filters section of the dataset properties.
This will effectively shift the filtering from the SQL to SSRS.
What you need to do is split your string in the database. What is being passed to your query is 'Test 1, Test 2' as a complete string, NOT 'Test 1' and 'Test 2'. This is why a single value works, and multiple values do not.
Here is a really good link on how to split strings, in preparation for your scenario. The function I most often use is the CTE example, which returns a table of my split strings. Then I change my SQL query to use IN on the returned table.
In your example, you will want to write WHERE Test_Number IN (SELECT Item FROM dbo.ufn_SplitStrings(#NewParam) , where ufn_SplitString is the function you create from the link previously mentioned.
This is what I did and it works well to me. You can try it also.
=sum(if(Fields!Business_Code.Value = "PH"
and (Fields!Vendor_Code.Value = "5563"
and Fields!Vendor_Code.Value = "5564"
and Fields!Vendor_Code.Value = "5565"
and Fields!Vendor_Code.Value = "5551")
, Fields!TDY_Ordered_Value.Value , nothing ))

Constraining columns with each other in SSRS

I'm learning MSBI tools and came across a requirement in SSRS wherein we need to constrain report parameter 2 with parameter 1.
i.e. if I have parameters setup on [AdventureWorks2008R2].[HumanResources].[Department] table and if I select "Group Name" (Parameter 1) as "Manufacturing" then the "Name" (Parameter 2) drop down should populate only relevant values("Production" and "Production Control") specific to selected parameter 1 value ("Manufacturing")
I have been exposed to OBIEE tool and know that under this tool we have a "constrain" option under Prompts to achieve the same.
I could come up with the following solution under SSRS:
Created two datasets for each of the parameters with below queries.
Parameter 1 (GroupName)
SELECT DISTINCT GroupName
FROM HumanResources.Department
Parameter 2 (DeptName)
SELECT Name
FROM HumanResources.Department
WHERE (GroupName = #GroupName)
This worked. However if there are 5 such parameters which are to be constrained then with this solution I would have to create 5 such datasets and accordingly build the WHERE clause for each parameter.
Is there a better solution to achieve the same (there should be..)
I'm new to SSRS and therefore would appreciate any pointers here.
Thanks!
What you are talking about is called "Cascading Parameters" and they are implemented exactly as you did. You can read about them here:
Adding Cascading Parameters (SSRS)
So if you need to implement 5 of such parameters you have to write 5 queries for 5 datasets each one containing WHERE referencing parameter defined in previous dataset

Textbox to Get Specified Value from dataset in SSRS

I want to get a specific row value from a specific dataset using the First function in SSRS.
The dataset returns values like:
From To Value
1 30 10
30 60 20
60 100 30
Also:
I have a parameter that has an integer value from 1 to 100.
I want to get the dataset value if the parameter is between From and To and
I want to put this value in Textbox not in a Tablix.
You can try this Expr in your textbox:
=First(Fields!FieldName.Value, "YourDataset")
Example:
=First(Fields!Name.Value, "GetName")
It worked for me.
This should work:
Use the parameter as two filters on the dataset, >= From and < To.
Then an expression in your text box like =First(Value.Value, "dataset").
You should first create a list (in order to relate to it a DataSet).
Then, inside the list, put a textbox and define the following expression:
=First(Field!Value.Value)
This will do the job :)
SSRS is a reporting tool not an application. You can either write your queries or SP to receive the value before generating the report, or otherwise i would recommend an application with a exporting functionality (you can even manipulate this to use SSRS to export your final report). Third party tools like Telerik can also maybe help.

how to pass this information to SQL Server 2008?

Please visit http://www.stumbleupon.com/
It asks you what interests you.
I would have a similar thing on my website except I would need to have 4 of such non-identical blocks. When a user has not selected anything in a section, it means s/he has selected all of them. 1 of these blocks would have around 10 options while the other 3 would have 2-3 options each.
How do I pass what all a user has selected to SQL Server 2008? Please note I would have 4 sets of multiple params, and these 4 sets are basically 4 different columns in the row all containing integer id's. If and only if one param per section was passed, I would have done something like
select * from table where column1 = #param1, column2 = #param2 and so on.
but that is not the case, because a user could pass multiple values for #param1 and so on.
I can get this done, but I want to know the most efficient way.
If my post is not clear, please let me know.
EDIT:
Basically, if I am storing articles in the database, I have 4 columns in the row, each integer. And I want to query these articles based on these 4 columns, but the catch is to pass the multiple values for these columns NOT just one value per column.
If your client language supports it, you should look into table value parameters:
http://www.sqlteam.com/article/sql-server-2008-table-valued-parameters
if you can't use those, look into one on Arrays and Lists in TSQL:
http://www.sommarskog.se/arrays-in-sql.html
this is a very comprehensive list of dynamic search conditions in TSQL:
http://www.sommarskog.se/dyn-search.html
this may also help:
Sane/fast method to pass variable parameter lists to SqlServer2008 stored procedure
Try dumping all parameter values into the XML, then pass it to SQL Server, then parse it in the query.
Won't such a query work in this case?
SELECT *
FROM TABLE
WHERE COLUMN1 IN ( #param11, #param12, #param13 )
AND COLUMN2 IN ( #param21, #param22, #param23 )
and so on.

Resources