I'm fairly new at stored procedures and using them with SSRS.
I want to create a simple SSRS report using a Stored Procedure with a parameter allowing multiple values (results from a separate Procedure)
I have 2 simple stored procedures as Follows.
Create Procedure WO
#STARTDATE datetime, #ENDDATE datetime, #DISTRICT varchar(25)
AS
SELECT A.WO, A.CUST, A.DISTRICT, A.COMPL_DATE
FROM WORK_ORDERS A
WHERE A.COMPL_DATE between (#STARTDATE) and (#ENDDATE)
and A.DISTRICT_NAME in (#DISTRICT)
Create Procedure DISTRICT
AS
SELECT B.DISTRICT_NAME
FROM DISTRICTS B
In my SSRS report, I'm reporting the WO Procedure using the result(s) from the DISTRICT procedure using a "DISTRICT" parameter with the available values from the DISTRICT procedure (ALLOWING MULTIPLE VALUES). Also I've used the DISTRICT Parameter in the Parameters of the WO procedure.
This works when selecting one District, but not when I select multiple districts. Anyone out there willing to help?
If I'm not mistaken, the problem is handling the string parameter #district - presumably, SSRS is passing something that looks like "N,S,E,W", assuming 4 districts were selected - the stored proc would then come out as: ...A.District_Name in ('N,S,E,W')... which would only return a value if there were a district name 'N,S,E,W'. So, I think you need to split the incoming value and reassemble it as 'N','S','E','W'. Unfortunately, there's no SPLIT function in SQL Server, so you either have to write a UDF (there are several posts available) or make a CLR call.
Here's a clever (and untested) thought - try Replace(#District,',',''',''') (I think I got the quotes right...)
Related
I've used the join function to create a string out of an array of variables.
=Join(Parameters!Variable.Value, ",")
Let's say that the expression is using a parameter called Variable to an expression called varstring. As well as to verify that it's working correctly, I created a text box in my report to show me the output of varstring. It looks like this:
123,456,789
Now I'm passing that string as a variable to a stored procedure. The code in the stored procedure would look similar too:
Declare
#var varchar(30) = varstring;
Select * from table where value in (SELECT * FROM string_split(#var, ','))
Lastly, the Variable parameter in SSRS is set to allow multi-values.
When I run this though, my report only returns records associated to the last string split. So in the above it only returns records equal to 789 in the report.
When I hardcode the values into the stored procedure, it returns records where value equals 123,456, and 789.
What am I missing?
Is there a way to pad numbers in a multiple value parameter to make them have 8 characters in length? Right now I am using:
=Right("00000000" & Parameters!Accounts.Value,8)
in an invisible parameter then that parameter is passed to the in part of my query. When I have, multiple values unchecked it works properly, but as soon as I turn on multiple values I get an error
The DefaultValue expression for the report parameter ‘ActualAccounts’ contains an error: Operator ‘&’ is not defined for string “00000000” and type ‘Object()’.”
I want the user to be able to paste in a list of accounts like:
93874
93932128
3838
And then it queries the database as
00093874
93932128
00003838
Here is an approach that could work for you.
First, grab the udf_Split user-defined function (UDF) from the this question's answer. With this UDF you to pass the delimited string as a parameter, and it will return a table with a row for each value. This is how SSRS sends the data to SQL Server when it comes to multi value parameters.
Once you have that in place, then all you need to do is use that UDF in the following manner.
DECLARE #ActualAccounts varchar(100) = '93874,93932128,3838'
SELECT RIGHT('00000000' + RTRIM(LTRIM(Value)), 8) AS Accounts
FROM dbo.udf_Split(#ActualAccounts, ',');
Results:
Accounts
--------
00093874
93932128
00003838
Then you could use this in the SQL for the report
SELECT *
FROM Accounts
WHERE AccountNumber IN (SELECT RIGHT('00000000' + RTRIM(LTRIM(Value)), 8)
FROM dbo.udf_Split(#ActualAccounts, ','));
This answer assumes the name of the SSRS parameter is ActualAccounts. You should be able to fit this into a stored procedure, if that is what you are using. It should work in straight SQL if you have that embedded in the RDL, too.
Hope this helps out.
I have certain parameters in the crystal reports which would be passed directly into Stored procedure in sql . Parameter A has 5 values and I want to enable them for users so that when user opens up the report , he is able to select any number of parameters values in the drop down based on which report data would be filtered.
Please advise
For a parameter to select multiple values you need to enable the option allow multiple values to true.
As I know, in designing a report in Crystal Reports you can use a simple SELECT like this:
SELECT CAST(0 AS int) AS Id, CAST(0 AS varchar(0)) As Name
That will give you two fields like Id, Name and you can use them in your report.
Now, you can use that type of SELECT statement instead of running a stored procedure like EXEC SP1 #P1 = P1, #P2 = P2 just inside of your report designer and running that stored procedure when you are filling the Data-Set of your report.
I have a SSRS report that pulls data using a stored procedure. Two of the parameters on this report have default values stored (i.e. Role:Student, ID:Guest)
One of the parameters is a dropdown that is filled with a different stored procedure using the two parameters mentioned above. When I set the parameters for this new dataset to #Role and #ID, I get a rsInvalidReportParameterDependancy error. I have changed the order of the two parameters with no success. Any thoughts on how to resolve this?
It sounds like you have three parameters but you have only mentioned changing the order of two. In my head, I would think that Role and ID must come first in your list of Parameters. I'd start by sorting those to the top. Then you should be able to add in your third parameter, populated by the result of those two. Can you provide any more information of what you've tried or what doesn't work?
The answer to this question has been posted before. However, I did not realize that I had the same issue. The answer has to do with the parameters not being in the correct order. For instance, I had 6 parameters on the main report. Parameters 5 and 6 were hidden to the user. Parameter 3 contained a dropdown that was populated by a stored procedure that needed #Param5 and 6. Even though parameters 5 and 6 had default values, it would not work until I moved param5 and 6 before param3.
I changed the order of the parameters on the report's main stored procedure and then change the parameters order in the report. (I ended up doing this using a text editor.)
#param1
#param2
#param3
#param4
#param5
#param6
Is there a way to do "and" "or" filters in SSRS 2005?
I have a table pointing to a dataset (stored procedure) that looks like this:
name type amount
License Plate Credit fees ($150.00)
Lieu Tax fees $1,012.12
Finance Tax City taxes $1,839.90
Finance Tax County taxes $306.65
Finance Tax State taxes $3,434.48
The user would like to see all rows with:
type = 'taxes' or
type = 'fees' and name = 'Lieu Tax'
The reason I need to do this in the report and not in the stored procedure is because
we will be creating multiple reports pointing to the same stored procedure depending on
how each client wants to lay out the display and business rules.
Requirements Clarification
I was hoping there was a way to do it in the report instead of the proc. The plan is to have many custom reports pointing to the same proc with different requirements. The idea was for report builders (who don't know SQL) to create the reports instead of us programmers always having to get involved.
Each possible condition combination is either a pass or a fail. You use a SWITCH to evaluate each possible condition, and return a 1 or a 0. Then you use an "=" and "=1" in the filter condition.
=SWITCH (TYPE = "TAXES", 1,
TYPE = "FEES" AND NAME = "Lieu Tax"), 1,
1=1, 0
)
You can handle your entire filtering in a single expression this way. Works like a charm.
You should modify your stored procedure to accept parameters like type and name and then from your report you should invoke this stored procedure with the right values based on user requirements. Reporting services reports do have a feature called report parameters. Therefore, you should convert your report to accept the type and name parameters from the user & pass this onto the stored procedure. To create multiple reports for multiple users, you will just create multiple linked reports from this one template report with different parameter values.
Change proc to be
SELECT
xxx
FROM
xxx
WHERE (a.id = #aID OR #aID IS NULL)
AND (b.id = #bID OR #bID IS NULL)
AND (c.id = #.....
Just pass in either a value or NULL from the report
Yes there is.
Open the table properties for the table or matrix with the data.
There's a 'filters' tab there allowing you to filter data after the query has been executed.