SSRS 2012 - loop through one Parameter - sql-server

I have a SSRS Report which is using a dynamic SQL Server stored procedure with one parameter only and it works fine.
I want to create a report which whenever I run it, it loop through multiple values for that parameter and creates multiple results, instead of doing that manually.
I found below links from Stack Overflow, but none of them help a lot.
Link1
Link2
I also read some I can use List in SSRS, but I am not sure how to do it in my case.
Any idea?

This might not be an answer as you have not provided enough detail, but I'm sure this idea will help you.
I think you'd better to change your stored procedure to accept multiple values and in your procedure return multiple result sets or combine record sets for each parameter value.
For example, assume you have procedure Proc1, which gets a number as parameter.
create procedure Proc1 (#aYear int)
as
begin
select *
from Orders
where OrderYear = #aYear
end
Now, what you need to do is changing your procedure to accept multiple years (for instance as a comma separated value)
create procedure Proc1 (#csv_years varchar(100))
as
begin
-- use some function or techniques to split the #csv_years
-- and store it into a temp table #years ([Year] int)
-- now, join your main data with this #years table
select #years.[Year], Orders.*
from Orders
inner join #years on #years.Year = Orders.OrderYear
end
Now, your are returning the result for multiple years and you can show that in your report by grouping on [Year] column

You need to use data driven subscription. I had cases with dynamic parameters and the only solution was data driven subscription.

Related

SSRS Dataset column not refreshed when using stored procedure with multiple Select and parameters

I have a report with a lot of datasets, and I want to make it easy for maintenance. I created a stored procedure that runs multiple select statements and accesses the data with parameters.
Sample stored procedure:
CREATE [dbo].[sp_test_report]
#inputReportPart varchar(20)
AS
BEGIN
IF (#inputReportPart = '1' OR #inputReportPart = 'debug')
BEGIN
SELECT a, b, c
FROM table1
END
IF (#inputReportPart = '2' OR #inputReportPart = 'debug')
BEGIN
SELECT q, w, e
FROM table2
END
END
But when I implement it to SSRS, the default column always use the first query even the parameter value is different.
Result:
Dataset view
Dataset1 property
Dataset2 property
I try to rebuild solution, refresh field, and delete .rdl.data but it doesn't work
Any solution other than add the column manually into dataset?
i want to make it easy for maintenance then i created stored procedure that store multiple select
That won't make anything easier. Use a different stored procedure for each dataset.
SSRS doesn't actually run your procedure to determine the dataset schema. So it will always discover the first resultset in the procedure, whether it uses FMTONLY or the newer sp_describe_first_result_set.
If you really wanted to make this work you'd have to add WITH RESULT SETS to your Dataset query which enables the client to specify the resultset metadata.

Difference between fetchrow_array and fetchall_arrayref when working with stored procedures

I have recently used both to fetch result of a stored procedure. I have noticed that fetchrow_array returns the output of rows I select inside the stored procedure. Whereas fetchall_arrayref returns status of the stored procedure in the last row, along with selected rows in stored procedure.
Why we have this difference and is there a way to avoid getting the status of stored procedure inside fetchall_arrayref?
My stored Procedure
CREATE PROCEDURE [dbo].[check_date_proc]
#dateStart DATETIME
AS
BEGIN
SELECT
check_date
FROM
date_Table
WHERE
data_date = #dateStart
END;
GO
and i call it like this
exec check_date_proc '20160920';
fetchrow_array returns
20160920
fetchall_arrayref returns
20160920
0
Thanks
Depending on which database type you are working with (and depending on the Perl DBD driver), it is likely that you are getting multiple result sets.
There is a way for processing multiple result sets (For ex., if you have executed two statements which have resulted in two result sets; and you want both of them).
Look here for sample code.
Since, in your case, you want to ignore the status of the stored procedure, you may feel it convenient just to fetch the results as hashes (all rows in one pull OR each one one by one), and then use the names of the columns to get the data.

how to select one or all value from a table using stored procedures parameters

I have a problem when I want to select more then one value in SQL Server. I found a lot of examples with SQL Server Reporting Services but I want to use this stored procedure in a Windows form application.
I have one parameter
#emp nvarchar(50)
select * from table
where crdname = #emp
This one returns table for a single crdname, but I have a situation when I need the table with all crdname.
I have a solution using C# and 2 stored procedures, one procedure for all crdname and one procedure for a single emp, but it's a lot of code for something that I'm missing.
This depends on the scope of the requirements. Do you want it to be one or all? Do you want to be able to do multiple values? For the one or all scenario:
Have a way to pass NULL to the stored procedure then change the WHERE to WHERE (#emp is NULL OR crdname = #emp).
When the parameter is NULL, the WHERE will evaluate to true and you will get all records. When the parameter is not NULL, you will pull the single value you are looking for.
For the multiple values scenario:
Change the = to IN. You might have to create a string split function since you are passing in nvarchar for the parameter.
Use the same stored procedure, but add a condition on the parameter:
CREATE PROCEDURE dbo.YourSP #emp INT = NULL --I assumed the data type
AS
BEGIN
SELECT *
FROM YourTable
WHERE crdname = #emp
OR #emp IS NULL
END;
Then when you want all the results, then call the sp like this:
EXEC dbo.YourSP
And when you want one, then do this:
EXEC dbo.YourSP 1234
You can combine both stored procedures into one, by passing in null for #emp
select *
from table
where #emp is null or crdname = #emp
If #emp is null, then the first condition is true and all rows are returned.
I would suggest two stored procedures, because the above approach is a bit convoluted. But that's just me :)
If you find yourself writing alot of sp's for your winform app, then maybe an ORM would help - e.g. Entity framework?

Help me with My Stored Procedure

In this procedure, I want to delete those number which coming in #msisdn from Master Table and insert into tblDeactive:
ALTER PROCEDURE [dbo].[msisdn_deactivition](#msisdn varchar(1024))
AS
BEGIN
SET NOCOUNT ON;
insert into tblDeactive SELECT * from msisdn_master where msisdn in (#msisdn);
delete from msisdn_master where msisdn in (#msisdn);
END
...but it does not work?
You can't use a single query parameter in place of a list of values. One parameter = one value.
See Parameterize an SQL IN clause for several solutions and lots of discussion about using parameters with the IN( ) predicate.
You need to use a split function, that will split your delimitted #msisdn into a table to select from.
Have a look at this link
How to pass a list of values or array to SQL Server stored procedure?
IN in TSQL needs to take multiple arguments per item. At the moment, if you pass it "123,456,789", it is looking for the single row with value "123,456,789". It isn't looking for "123", "456" or "789".
Look for something like a split udf - plenty on the internet. This takes your delimited string and returns a column of values. Typically you then join the the udf results (rather than use IN).
DON'T use EXEC here, as you shouldn't trust the input.

SQL Server stored procedure issue calling another stored procedure

Here's a issue I have with a stored procedure (using SQL Server 2005), inside this stored procedure it calls another stored procedure putting the data into a temp table.
INSERT INTO #tmpTable (Column1, Column2, Column3)
EXEC psp_rptInsideStoredprocedure 2
This inside stored procedure has a mode parameter that determines which columns get passed out. In this mode (Mode2) only 3 columns get passed out, when this inside stored procedure is used for another report (Mode1) 4 columns gets passed out. Sometimes the parent stored procedure complains about trying to insert the 4 column and sometimes not.
I know it's always passing in mode 2 but it's like SQL Server knows that sometimes this stored procedure has passed back 4 columns.
Any thoughts on a solution?
Thanks
Don
Make the child procedure always return the same number and type of columns, use NULL if necessary. If the results are so different for the two versions, that you can't combine them like this, you should consider making two different procedures.
I'm just guessing at your procedure, but you could create a #ResultSet temp table and populate it based on your type parameter, but always return all columns, etc. If you are returning lots of rows this becomes bad, as the temp table is overhead. in that case, just make sure your result sets return the same number of columns:
IF #version=1
BEGIN
SELECT col1, col2, NULL FROM ... WHERE ...
END
ELSE
BEGIN
SELECT col1, col2, col3 FROM ... WHERE ...
END
Then when both parents call it, accept all the columns, but they can ignore what they don't need.
Daisy chaining stored procedures is generally not a great idea. I would remove the call to the sproc and type out the t-sql for exactly what you need. If your really set on calling another sproc. Make a new sproc that does exactly what you need for this one situation.
It's all about the Single Responsibility Principle

Resources