How to Pass Report Parameter as if it were a Query Parameter? - sql

I am trying to make a report that allows a user to input an ID and then check this ID against several payee tables to see if the value already exists. The code requires an input and then displays a value of 1 if the code already exists and a 0 if the code is available for use. This works in the SQL code when setting the ID as a query parameter, but does not appear to work when creating an SSRS report and passing a report parameter through; the report displays no data.
I have tried adding a tablix column to display the parameter for validation. This field remains blank when attempting to pass a report parameter, but does properly display when hardcoding the query parameter.
DECLARE #id varchar(8)
SELECT #id,
CASE
WHEN #id IN (
SELECT id
FROM payee_1
)
THEN 1
WHEN #id IN (
SELECT id
FROM payee_2
)
THEN 1
WHEN #id IN (
SELECT id
FROM payee_3
)
THEN 1
ELSE 0
END as validation
The SQL query produces the results I would like to see in the SSRS report. It simply shows the input ID and a 1 if not available (or a 0 if available). When input into SSRS, the report parameter never passes through as if it were a query parameter and my report ends up being blank.

When your report is open for edit (Visual Studio), in the "Report Data" panel (docked on the left, by default), expand "Datasets". Right-click your Dataset and choose "Dataset Properties".
In the "Dataset Properties" window, on the left, click "Parameters". The parameters shown are populated from your query. If you have a parameterized query, then this will give you the option to bind your query parameters ("Parameter Value") to your report parameters, or set up other defaults or formulas, etc.

Related

Report Builder - 3 Parameters all in one Query. Forward dependencies are not valid

I have an issue with building my report, I have 3 params and all 3 are in a query, they are supposed to get their values from the "drop down menus" before running the reports and are used in a where cluase to get specific stuff from the database. However I can seem to get it to work.
Example Query
Select * from [Table]
Where ID = #ID and DateFrom = #DateFrom and DateTo = #DateTo
order by ID
This is the query, I tried changing the orders of the params but it doesnt work.
Error:
The report parameter 'ID' has a DefaultValue or a ValidValue that depends on the report parameter "ID". Forward dependencies are not valid.
Based on the error you reported...
"Error: The report parameter 'ID' has a DefaultValue or a ValidValue that depends on the report parameter "ID". Forward dependencies are not valid."
I suspect your ID parameter's valid values are taken from a dataset query. The dataset query uses a parameter called #ID . You cannot populate values for a parameter if the parameter you are trying to populate is required by the query.
If you are trying to get a list of available ID's to populate the ID parameter drop down then you need to create a separate dataset for this. The dataset query would be something simple like.
SELECT DISTINCT ID FROM [Table] ORDER BY ID
You can then change your ID parameter's "Available Values" dataset to point to this new dataset.
If this does not help, show you report design (the parameters at least), the parameters properties and the dataset queries for each one. Your issue should be clear once all that is visible.

Making parameter Optional with multivalue parameter in SSRS?

I have a parameter in which I have selected "Allow Multiple Values" at the same time how can i make the same parameter as optional. For example if customer wants to leave the parameter value as blank he can on the other hand if customer wants to select any value then it is allowed. In Case any of the parameter is left blank the code should run
I am using below mentioned query where i want to make enrollment_type the parameter where i want above mentioned conditions
Where (School IN (#school))
AND (School_Year IN (#schoolyear))
AND ((EnrollmentType IN (#EnrollmentType)) OR ('' IN (#EnrollmentType)))
OR (Grade IN (#Grade))
you could in ssrs >> parameter properties on general tab select option for allow blank values is sql you need :
Where (School IN (#school) or #school='' )
you can also do the same thing with allow null values
Where (School IN (#school) or #school is null )
I guess you are trying to make your enrollment_type parameter as optional to the users. If you are looking to make no filter on enrollment_type by default, you can set the default setting of the parameter.
If you are populating your parameter value from query, set the default value exactly same as available values i.e. parameter>Default Values>Get Value from Query>Dataset>Value
By setting both the available values and default values same. You will not be asked to select any value for enrollment_type and the Select All option will be automatically checked.
You need to add EnrollmentType IN (#EnrollmentType) in your dataset.

Excel parameters from SQL Query from ODBC "Error - No Value given for one or more required parameters when using question mark `WHERE XXX = ?`

I have an Excel file with an SQLOLEDB connection to a MS SQL server,
When I do this with an Oracle link I can simply put WHERE XXX = ? in my query to prompt for values in Excel, however doing it with MSSQL I am getting the following error:
No Value given for one or more required parameters
When trying to parameterise a basic excel query from a value to ?
If i use values like this I get results:
SELECT *
FROM srv.stats
WHERE srv.stats.[year] = '2016'
AND srv.stats.[quarter] = '1'
When I add in the parameter ? I get the above error.
SELECT *
FROM srv.stats
WHERE srv.stats.[year] = ?
AND srv.stats.[quarter] = ?
I want to be able to do this without using VB.
Since MS SQL sources from ODBC connections don't inherently allow you to use the WHERE xxx = ? code in your SQL query (unlike Oracle connections) you need to spoof Excel to allow parameters from a MS SQL data source by building a Microsoft Query and then overwriting it.
Open a new Excel file and go to the Data tab, choose the From Other Sources drop down and select From Microsoft Query.
The Choose Data Source window will appear. Choose an existing datasource or set up a new connection to your server by selecting <New Data Source> and click OK.
Once done you will see the query wizard window open to select tables and columns, as you're going to be adding your own SQL query later just select one table that is in your query and add it to the Columns in your query: section by using the > button. For the next 2 windows just click Next and then finally Finish.
You will then be prompted to select how you want to view the data in the Import Data window, first of all click the Properties... button.
And then the Definition tab, in the Command text: box you will have a SELECT statement, below there you will need to add WHERE clauses for the amount you have in your actual query. These need to be added in the format of:
WHERE 1 = ?
AND 2 = ?
AND 3 = ?
Once this is done click OK to go back to the Import Data window and select your output type; Table, PivotTable report or PivotChart and PivotTable Report depending on what how you want to display your data.
You will then be prompted to enter a value for each parameter. If you are getting these values from Cells choose the location of them now in the order you will be putting in your actual parameters. When you have entered your parameter sources go back to the Data tab and click connections and then into the definition tab.
Now in the Command text: box paste in your actual SQL query with your parameters in the WHERE clause = ? and in the same order your defined the sources and click OK, Your data should now populate as it usually does with your parameters being used.
There is no way to prompt for inputs directly in SQL Server in the same way that you can in Access for example. You can make the values that are passed into queries like this.
DECLARE #year SMALLINT
SET #year = 2016
DECLARE #quarter TINYINT
SET #quarter = 1
SELECT *
FROM srv.stats
WHERE srv.stats.[year] = #year
AND srv.stats.[quarter] = #quarter;
You then just need to find a way that suits your solution (Excel?) to pass these. This might, for example, take the form of a stored procedure.
CREATE PROCEDURE testProcedure #year SMALLINT, #quarter TINYINT
AS
SELECT *
FROM srv.stats
WHERE srv.stats.[year] = #year
AND srv.stats.[quarter] = #quarter
GO;

Set default value for parameter in pentaho report designer

I am creating a report in pentaho report designer and need some help setting default values for a parameter that I've created.
One of the parameters labeled date fetches data from the date column of a table. While I am able to view all the dates in the drop down list, I am unable to find a way in which I can set the default value of this drop down to all (meaning all the dates together).
Is there a way in which I can set the 'all' value as default?
Assuming that you get the values for the filter from a JDBC connection in PRD, you can write a query like this. (I have used Postgresql).
This will load 'All' as the first values in the drop down and other distinct date values from your table. (Do the casting properly)
SELECT 1 AS sort,'All' AS date
UNION
SELECT DISTINCT 2 AS sort,
tablename.datecolumn::date AS date
FROM
tablename
ORDER BY sort
Then in your parameter that is shown to the user to select the date, enter All in Default Value field and select the query that we have written.
Next assuming that you use a KTR to retrieve data to your report, you can include the following query in a 'Table input' step,
(tablename.datecolumn IN (SELECT CASE WHEN('${date}' = 'All' OR '${date}' IS NULL) THEN tablename.datecolumn ELSE '${date}' END))
Hope this will help. If you have any further issues please comment below. Because this has worked perfectly for me.

For SSRS in Visual Studio 2008, how can I make a variable accept multiple values as well as a single value?

I'm making a report with Visual Studio 2008, pulling the info from a table in a database. Let's say I just want to show the Type of employee (which is represented by an int, say 1-10) and their Name. Then my dataset would be this query:
SELECT Type, Name
FROM Employees
WHERE Type = #Type
This is SSRS, so I do not need to declare or set the variable (correct me if I'm wrong). When I run the report, it will give me an option to type in an int for the Type and it will create the corresponding report. My question is how can I set it so that I can type 1,2,3 in for the Type so that I get a report with those types of employees? So having the int variable be able to accept a list of ints or just one int. Essentially the "resulting query" from that example would look like this:
SELECT Type, Name
FROM Employees
WHERE Type = 1 AND Type = 2 AND Type = 3
On the left side in the Report Data window under the Parameters folder, right click the variable, hit Parameter Properties, then check 'Allow Multiple Values' and select the correct datatype in the dropdown. I'm not sure why it decides to make a dropdown when you run the report, and you have to enter the values each on their own line instead of separated by commas, but it seems to work fine. Then just change the WHERE clause in your dataset from WHERE Type = #Type to WHERE Type IN (#Type). I don't know if the parentheses are necessary.
Also if you create a separate dataset that will present certain values you can have those show up instead of having to type them out. For example, create a dataset that contains this query
SELECT DISTINCT Type
FROM Employees
ORDER BY Type
This will create a list of distinct values for type that you can check/uncheck. You can make it more complex obviously as well.