Sample query for a dataset.
if(previousParameterValue=true) exec StoredProcedure1 null, null
else exec StoredProcedure2 null, null, null, null
If I use this format, only the result of the first SP is available for the parameter and the list is getting displayed only for the first condition. I need to generate a list during runtime based on the input selected in previous parameter. Is there a way to implement this functionality in SSRS? Will making the number of output, name of the returned columns same from the two stored procedures help achieving this functionality?
Your dataset query must always return the same structure, the same columns names with the same data types.
When the above is true it should work fine.
Personally, I create a temp table, dump the results of the SP into it and then return the temp table. That way, even if the field names are not identical or the datatypes are not quite the same (int vs smallint etc) then SSRS will only ever see the temp table structure and it will be fine.
For example.
CREATE TABLE #t(CustomerID int, CustomerName varchar(100), ValueA float, ValueB float)
IF #myParamater = 1
BEGIN
INSERT INTO #t(CustomerID, CustomerName, ValueA)
EXEC myProcA
END
ELSE
BEGIN
INSERT INTO #t(CustomerID, CustomerName, ValueB)
EXEC myProcB
END
SELECT * FROM #t
The Dataset Query's Expression Builder can be used to create an expression based on a parameter.
An IIF statement could check the parameter and return the text to run.
=IIF(Parameters!myParamater.Value = 1, "EXEC StoredProcedure1", "EXEC StoredProcedure2")
To make this work, the stored procedures should return the same fields. The dataset should be created using a single stored procedure without using the Expression Builder and then changed to the expression after the fields have been created and saved.
I am using this code at the top of the SQL in my dataset for an SSRS report:
IF OBJECT_ID('tempdb..#ValidUsers') IS NOT NULL DROP TABLE #ValidUsers
CREATE TABLE #ValidUsers
(
ValidUsers Varchar(100),
)
Insert Into #ValidUsers SELECT 'ABC\UserA'
Insert Into #ValidUsers SELECT 'ABC\UserB'
Insert Into #ValidUsers SELECT 'ABC\UserC'
Then, I have this code that, at run time, pulls the current UserID into a variable #CurrentUser and I check if #CurrentUser is in my #ValidUsers temp table:
IF EXISTS (SELECT * FROM #ValidUsers WHERE ValidUsers = #CurrentUser)
Begin
... run SQL to retrieve data ...
Else
... don't retrieve the data ...
End
This is working just fine and only the users I specifically insert into #ValidUsers are able to run the report.
Is this acceptable - or is it a bad/insecure way to limit who can run an SSRS report?
If it is not good, what is the recommended way to restrict who can run a report?
You could create an user dataset in your report, then filter it by where name=#CurrentUser (set #CurrentUser's default value to User!UserID) in report. When the current user is in user dataset, the report will show, if its not in it, the report will show blank.
You could refer to this post for details.
Select tblppmp.idn
,tblppmp.total_item as a_total
,tblRequest.Quantity as b_total
,tblppmp.total_item - tblRequest.Quantity as itemsleft
FROM ppmp.dbo.tblppmp
INNER JOIN
(SELECT
tblrequest.idn
,sum(tblRequest.Quantity) AS Quantity
FROM ppmp.dbo.tblrequest
WHERE tblrequest.dr_year = 2015
GROUP BY tblrequest.idn) tblrequest ON tblppmp.idn = tblrequest.idn
Above is my code, how to create stored procedure and the value of the dr_year may change depending on the textbox or combobox selected by the user..sample is where tblrequest.dr_year = "Textbox1.text"
Whether the different value comes from a textbox or a combobox is irrelevant. It will always come into your stored procedure as a SQL parameter.
To create the Stored Procedure, simply execute the following on your database:
USE AdventureWorks
GO
CREATE PROCEDURE dbo.uspGetAddress
AS
SELECT * FROM Person.Address
GO
Replace the database name "AdventureWorks" in the USE command and the procedure name "uspGetAddress" in the CERATE PROCEDURE command with your database and procedure names respectively
Since you have a dynamic date value, you'll need to add the parameter. This changes your SQL code to look something more like this:
CREATE PROCEDURE dbo.uspGetAddress #Date datetime
AS
...
GO
Check out MsSQLTips for more info
I am working on SSRS Report.
Continuously getting bellow error does anyone have idea to sovle?
The value provided for the report parameter '' is not valid for its
type. (rsReportParameterTypeMismatch)
Using query something like below - My query type is Text
CREATE TABLE #Temp
(
....
)
INSERT INTO #Temp
(
...
)
SELECT ...
FROM ... joins TABLE....
WHERE d.Date BETWEEN #FromDate AND #ToDate
SELECT ...
FROM #Temp
.. joins TABLE...
WHERE ...
DROP TABLE #Temp
Image that shows the error.
Probably incorrect Date format on the server.
To resolve it, change the Date format on the host to en-us via Control Panel
I have a stored procedure that returns multiple resultsets just as below
CREATE StoredProcedure sp_MultipleDataSets
AS
BEGIN
SELECT EMPID, ENAME, JOB, SAL, DEPTID FROM EMP -- first result set
SELECT DEPTID, DNAME, LOC FROM DEPT --second result set
END
In BIDS, while creating a new report I configured the stored procedure for dataset. It creates the dataset ONLY with the columns returned from the first result set. It does not identify the second result set.
How can I create datasets for both the result sets from a stored procedure like above
Unfortunately, as the documentation explains here:
If multiple result sets are retrieved through a single query, only the first result set is processed, and all other result sets are ignored.
(Found via this question.)
Therefore, I suggest using one of two possibilities:
(1) Split the procedure into two separate procedures - one which returns data from EMP, and one from DEPT - and access the new procedures as two separate datasets.
(2) Union the two separate queries (with an additional column to indicate which query produced each row) and filter or conditionally format your report appropriately. The unioned query might look something like this:
SELECT EMPID ID, ENAME NAME, JOB JOB_LOC, SAL, DEPTID, 'EMP' SOURCE
FROM EMP
UNION ALL
SELECT DEPTID ID, DNAME NAME, LOC JOB_LOC, NULL SAL, DEPTID DEPTID, 'DEPT' SOURCE
FROM DEPT
I use a Parameter in the SP to pull multiple Result sets in SSRS all the time.
You have to separate them by IF statements in the SP and also you then have to HAND TYPE out the FIELDS in the SSRS Dataset setup.
Seems whacky, but it works...
Here is a example.
Stored Procedure(SP) has 2 Parameters defined
#OfficerID
#DatasetFlag
The #OfficerID is the Employee # that has to be passeed or ENTERED IN (SSRS Data input form)
The DatasetFlag is the way I control which IF statement is executed in the SP.
so here is the SP:
CREATE PROCEDURE [dbo].[StoredProcedureNameHere]
#OfficerID VARCHAR(7),
#DatasetFlag VARCHAR(60)
WITH RECOMPILE
AS
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET NOCOUNT ON;
BEGIN TRY
IF #DatasetFlag = 'EmployeeName'
Begin
SELECT [EmployeeName]
FROM [DatabaseName].[scema].[Employee]
where EmployeeBNumber = #OfficerID
END
ELSE
IF #DatasetFlag = 'Expect'
Begin
SELECT
[TerritoryName]
,[TestNumber]
,[RuleNumber]
,[Expectation]
,[TestCount]
,[PercentToGoal]
FROM [Database].[scema].[Table2]
WHERE OfficerID = #OfficerID
ORDER BY TerritoryID
,TestNumber
,RuleNumber
END
RETURN
GO
The REPORT has 2 Datasets, one I create and it will pull in the parameters and EmployeeName
the other I create and it pulls in EmployeeName as SSRS can only use 1 Result SET~!
BUT.... I fool it ....
but I simpley CREATE the FIELDS by OVERTYPING the EMPLOYENAME and then ADD the
rest (Query) So Edit the FIELDS in the PROPERTIES of the DATASET and put in the Select Column Names.
Then I ese the PARAMETERS menu (Dataset Properties) to put in an EXPRESSION of ="EmployeeName"
for the first Dataset and ="Expect" for the 2nd. I match up the #OfficeID on that screen too.
then when I run this... it asks for OfficerID (RS creates the input form)
and when I put in the ID and hit VIEW REPORT. Or we can all the RDL with the OfficerID, just like the SSRS form does, but in an ASPX page.
BOTH DATASETS are returned (it calls it twice is my assumption)
So NO whacky FILTERING on a UNION dataset or other tricks that I have to then deal with in SSRS, which is not fun... (seriously IIF?)
I have see one Stored Procedure where we have like 20 Parameters , so you can filter
the OUTPUT of the report at the SQL level, rather than a monster pull and filter at the report.
No can you simply create 20 Stored Procedures, WELL YES, but this way, all the code is in ONE locations, and of course it can use selects into TEMP Tables to merge tons of stuff,
and in the end simply POPULATE a table that is the RESULT SET..
As a programmer I find SSRS a bit whacky, but so far I am having fun, trying to find ways to get what I want, not what it offers...
Try something like that:
Create StoredProcedure sp_MultipleDataSets(#Param nvarchar(10))
as
begin
if(#Param == "first")
begin
SELECT EMPID, ENAME, JOB, SAL, DEPTID FROM EMP -- first result set
end
if(#Param == "second")
begin
SELECT DEPTID, DNAME, LOC FROM DEPT --second result set
end
end
Here is one trick for this. This follows idea to Union All all the results for one table and works with SSRS repost with table
For each separate query add one column where is showing purpose of the query For e.g. "name" or "address", this info is repeated for each line of query.
Then Union All the wanted queries.
At Visual Studio / SSRS reporting: Add the dataset which contains storedprocedure. Then from tools select the tablix and drag the wanted data to tablix columns. Then go to tablix row properties -> Row visibility. for there make the sorting clause with IFF function to show only rows with previously defined extra column at queries, for e.g. "Name"
Then make the second tablix and follow the same, now use the IIF function with "Address", then continue as many different tables needed.