I'm creating a report in SSRS using a stored procedure. My report is simple, basically pulls basic information of a client in different programs.
In my stored-procedure I have two parameters: #StartDate DATETIME and #VendorId INT = NULL.
I'm setting #Vendorid to NULL because I would like to get all clients in every program.
I have two options in my sproc:
IF (#VendorID > 0) --Select Program
BEGIN
INSERT INTO #Report
SELECT * FROM table1 WHERE VendorId = #VendorId
END
IF (#VendorID IS NULL) --All Programs
BEGIN
INSERT INTO #Report
SELECT * FROM table1
END
This report gives me the flexibility to get clients from different programs from the day they were enrolled. Hence, if I want to get all clients in every program I simply execute the sproc with the #StartDate parameter. I would like to do the same in SSRS. I just can't figure how to set the #vendorid parameter function the same way in my sproc.
Any insight would be helpful!
AS you know generally in SSRS you can have parameters as Query string or selected value
so
Just add
SELECT * FROM table1 WHERE #VendorId=-1 OR VendorId = #VendorId
To specify a custom default value here -1
Switch to Design view.
In the Report Data pane, right-click #VendorId, and then click Parameter Properties.
Click Default Values > Specify values > Add. A new value row is added.
In Value, type -1.
Click OK.
Preview the report.
You have drop down and you can use -1 as none selected.
https://learn.microsoft.com/en-us/sql/reporting-services/tutorial-add-a-parameter-to-your-report-report-builder?view=sql-server-ver15
Related
I am trying to create the report that would have action link in the text box that would update the record in database. First of all I've created datasource with the "Execute in single transaction" setting on. I've created dataset that calls the stored procedure with param:
Then specified the param:
I've created the table where the last column is "Action".
In the TextBox properties, in the Action menu set to go to the same report and set param value to the one from the table:
The thing is that it's working only once and if I click on the same field again then the procedure is not executed. I guess that param is not set, but I am not sure.
UPDATE:
After some playing I see, that variable is set so the problem is not in variable. Additionally if I click on another row then this row is updated as expected. After that I can updated the first row also, so the problem is that I can't update the same row twice in a roll.
CREATE PROCEDURE [dbo].[change_salesperson_status]
#code AS CHAR(5)
AS
BEGIN
INSERT INTO dbo.test VALUES (#code);
IF (#code = 'test') RETURN 1;
UPDATE dbo.salespersons
SET status_code = CASE WHEN status_code = 'I' THEN 'A' ELSE 'I' END
WHERE code = #code;
END;
GO
CREATE TABLE dbo.test
(
a VARCHAR(200)
)
UPDATE2:
I was able to workaround this. I've added internal datetime variable with default value =NOW() this forced SSRS to refresh report every time.
This isn't really an answer, it's a guide to how I would do this but too long to put as a comment.
I've done quite a few of these reports and never had any issues. I suspect your workflow is not quite right.
What I normally do is write a dataset query that accepts parameters that update the database unless the default values are passed.
I'm not sure what you are trying to do but if we take a simple example, creating a report called, say myReport which makes an employee inactive if a column is clicked then we might have a simple table
EmpID EmpName MakeInactive
1 Dave click
2 Bob click
The dataset would look something like this...
SET NOCOUNT ON
IF #EmployeeID > 0
BEGIN
UPDATE myTable SET Active = 0 WHERE EmpID = #EmployeeID
END
SELECT EmpID, EmpName FROM myTable
The myReport report would have a single parameter #EmployeeID in this simple case and the default value would be -1 so when we first run the report, no updates occur.
In the last column you would do as you have already, that is, set the action to call the same report (myReport) with the #EmployeeID parameter set to the EmpID from the first column.
That's all that I would do, I don't use single transactions or anything else, I just do as above and it works.
I have a SSRS report which accepts a parameter (Country_Name).
This is a weekly report and needs to be generated automatically by triggering the SQL agent job. So, I have created a data driven subscription for this report and created a SSIS package to generate the report from SQL agent job.
Now the problem is : Since it is a automated report, there is no interface to pass the parameter to report. So, I have created a table to hold the list of parameters to be passed and the path to place the generated report.
Table will have 2 columns (Country_Name & Report_Path) and hold values like (India \AB123C\India) (China,\ABC\China) etc.
Depending on the parameter passed, location of the report will change. So, I used looping in data driven subscription query to get the parameter and path. Though there are multiple parameters and path for that respective parameter are returned by the query, it is picking only the first result set and generates report only for the first parameter and its specified location.
I unable to find out the solution for this. Please find the below query I have used for your reference. It would be great if I can get a solution for this.
DECLARE #MinCount INT = 1
DECLARE #Country VARCHAR(100)
DECLARE #Path VARCHAR(MAX)
DECLARE #RecordCount INT
DECLARE #CurrentDate VARCHAR(10)
DECLARE #CountryList Table (ID INT Identity(1,1),Country_Name Varchar(100),Report_path Varchar(max))
SET #RecordCount = (SELECT COUNT(*) FROM Country_List)
WHILE (#MinCOunt < = #RecordCount)
BEGIN
SET #Country = (Select Country_name from Country_List WHere ID = #MinCOunt)
SET #Path = (Select Report_Path from Country_List WHere ID = #MinCOunt)
SET #CurrentDate = ( SELECT CONVERT(char(10), GetDate(),126) as currentSysDate)
Delete from #CountryList
insert into #CountryList (Country_Name,Report_path) Values (#Country,#Path)
select 'Country_Details ' +
#CurrentDate as filename
,'Excel' as RenderFormat
, (select Name from Master where
Reference_Name = 'USER_NAME'
) as sqlUserId
,( select Value from Master where
Reference_Name = 'PASSWORD'
) as sqlPwd , (Select Country_Name from #CountryList) AS Issue_Country,
(Select Report_path from #CountryList) AS filePath_cfonereports
SET #MinCOunt = #MinCOunt + 1
END
I have resolved this issue now by using a different approach rather than using looping in subscription code. I created a reference table to store to the list of parameters as well as the corresponding path. Since this report is called from a SSIS package, I have used a variable to get the list of parameters from the reference table and a foreach loop container with Execute SQL task to pass one parameter from variable at a time and invoke the subscription. So that the report will be generated for that parameter passed and in the path specified. This foreach loop container will loop n times based on the count of parameters in the variable
I am trying to create a Crystal Report within VS2015 but I am having issues with a stored procedure. When I run my report without a stored procedure it gets all the data required and all the groups and formulas work fine. Below is my main SQL code.
SELECT type, description, hire_status, fleet_no, location, date_starting, time_finishing, date_finishing, type, week_number, changes
FROM XXXXXX INNER JOIN XXXXXX ON fleet_no= XXXXXX.fleet_no
WHERE week_number= #weekNo
When I try and add a stored procedure to the report I only get a blank report, I lose all the data from the report. The stored procedure is as follows:
USE [xxxxxx]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [XXXXXXX].[getCurrentLocation]
#fleet varchar(50) = NULL
AS
Begin
SELECT TOP 1 (dbo.tbl_job_planning.location)
FROM dbo.tbl_job_planning
WHERE (fleet_no = #fleet) AND (date_starting <= DATEADD(week, DATEDIFF(day, 0, GETDATE()) / 7, 5))
ORDER BY date_starting DESC
end;
I pass the parameter to this stored procedure in the record selection.
I want the stored procedure to display a location on each line of the report. When I run the report with just the stored procedure I only get 1 value returned and not a value for each item in the report which is what I need.
you are getting 1 value back because you are specifying
SELECT TOP 1 (dbo.tbl_job_planning.location). Just remove TOP 1 or set the 1 to however many results you want it limited to , but certainly not one as you specified
I have a report that is has a multiple value parameter. It has available values (1, 2, 3, 4, 5, and blank). The blank value isn't NULL it is just an empty cell.
For testing purposes, there should only be one record showing up. The record has a blank value in the cell that I am using to filter on in my stored procedure.
In the parameter drop down, I see all of the options. If I choose the (Select All) option, I don't get the record I am wanting. However, if I choose just the blank value, I get the record I am wanting. I updated the record manually to "2". I choose (Select All) and I get the record. I choose only the "2" value and I get the record. If I remove the value again to make the value blank, I only get the record if I only choose the "Blank" option in the drop down.
I make a text box to output the value of the parameter and it is showing blank when it is selected either by itself or with other values.
What am I missing in either my stored procedure or in my report? Thanks in advance.
When you have a multiple select parameter, SSRS should send a comma separated list of values to your stored procedure. You are responsible for splitting these back out into something that you can join on.
In the report itself, you don't get the list of values in a nicely wrapped up string for displaying. I've had to use code to iterate over the values in the parameter.
I tested what SSRS is doing when you have a blank available value. I created a test report that had the six available options and then a stored procedure to force the selected values to be output:
CREATE PROCEDURE dbo.Test_RPT
(
#TestMultiSelect varchar(1000)
)
AS
SELECT #TestMultiSelect RVAL
In the report, I just had a single textbox that displayed this RVAL field.
If I put the blank option at the start, the output was 1,2,3,4,5. If the blank option was at any other location, it was included: 1,2,3,,4,5, 1,2,3,4,5,.
If you want to pass multiple values to Sql IN clause within a Stored Procedure from SSRS report dataset, slight changes needed in the Stored Procedure:
See the Sample WHERE clause in a SP
...
AND Courses.Type_Code IN (#Type_Code)
Add a SplitString() function to split the passed parameters based on a delimiter.
The body of the SplitString() will look like:
CREATE FUNCTION [dbo].[SplitString]
(
#pString VARCHAR(8000),
#pDelimiter CHAR(1)
)
RETURNS #Values TABLE (Value VARCHAR(50))
AS
BEGIN
DECLARE #xml as XML;
SET #xml = cast(('<X>'+replace(#pString, #pDelimiter, '</X><X>')+'</X>') as xml);
INSERT #Values
SELECT N.value('.', 'varchar(50)') as value
FROM #xml.nodes('X') as T(N);
RETURN
END;
Finally the IN clause will looks like:
...
AND Courses.Type_Code IN (SELECT * FROM dbo.SplitString(#type_code,','))
I have a Crystal Report (using CR 2008) that has three database queries (the below is sourced from the "Show SQL" menu option)
MYSERVER
"mydb"."dbo"."run_report";1 {d '2000-01-01'}, {d '2015-01-01'}, 'A12345'
MYSERVER
select SUser_SName() as [CurrentUser]
MYSERVER
select parent_name from containers where id = '{?#id}'
None of these queries depend on each other, although the first and last queries use the same input parameter - #id (which is A12345 in this example)
The above works fine when the run_report stored procedure returns > 0 results. When it returns no results (but no actual errors as far as I can tell), the second and third SQL queries don't appear to be executed as their results (which are inserted into the report as fields) are just blank.
I can see two possible approaches to the issue:
Reorder the SQL queries so that the ones that will always return a result run first, and the stored procedure (which may return no results) runs last; or
Somehow I make Crystal Reports continue on its merry way even if the stored procedure query doesn't return any rows.
...but I'm not sure how to achieve either approach.
I cross-posted on the SAP forums and got this answer.
There are two possible solutions
Make the stored procedure return a single row of all NULL values when there's no actual results.
Split the report up into one main report and two sub-reports. The Stored Procedure runs in the main report, and each of the other queries runs in its own sub-report
I used solution #1 (as it was less work in my case) and it worked fine. I changed my stored procedure into a SELECT INTO, with results getting put into a temporary table. I could then go:
-- Do the main SELECT INTO query first, then...
-- Check if temporary table is empty
IF NOT EXISTS (SELECT TOP 1 Id FROM #report WHERE Id IS NOT NULL)
BEGIN
-- If the source table you SELECTed from had NOT NULL columns,
-- you'll need to alter them prior to inserting your row of NULLS
ALTER TABLE #report ALTER COLUMN Name VARCHAR(255) NULL
-- Rinse and repeat for other affected columns
-- Then insert the row of NULLS
INSERT INTO #report VALUES(NULL, NULL, NULL, NULL, NULL, NULL)
END
-- Return the contents of the report
SELECT * FROM #report