Manually enter a parameter value or All by default in SSRS - sql-server

I'm trying to create a parameter in SSRS where the user has the option to either manually enter an id or else have the report return all records. This is how the parameter currently looks:
In this situation the user can manually enter one providerid, which works. But is it possible for the user to leave the providerid parameter blank and then by default the report will run for all records?
The providerid parameter is populated with a stored procedure:
Select distinct cast(providerid as varchar(25)) as providerid
from Table A
I've read other threads on here that appear similar, but I can't figure out how to get this work.

The standard way to do this is:
Add another item to your drop list called ALL with a special value
Select distinct
cast(providerid as varchar(25)) as providerid,
cast(providerid as varchar(25)) as providerLabel
from Table A
union all
select -1, 'All Providers'
Make sure you assign label and value correctly.
Allow for this special value in your stored proc:
where ...
and (providerid = #providerid or #providerid = -1)
and ...

Related

Convert SSRS parameter to Null

I have SSRS report that showing Marketing Features. we have in the MF a field called "Test", the values are "PSI 1" or PSI 2" and also the field can be empty.
I added a dataset for the Test parameter with this query:
SELECT DISTINCT Corp_Test
FROM [Tfs_Warehouse].[dbo].[CurrentWorkItemView]
WHERE ProjectNodeGUID = #ProjectGuid
AND System_WorkItemType = 'Marketing Feature'
UNION
SELECT 'Null'
ORDER BY Corp_Test
Now in the report, the values are PSI 1 PSI 2 and Null:
In the Main Query I filtered the results according to the parameter like this:
where COALESCE(Corp_Test, 'Null') IN (#TestParam)
The report works fine, and if I selected all values I get also Marketing Features with empty Test field.
My question is:
Instead of Null on the dropdown, I want it to be written No PSI and actually in the main query it will be Null. is it possible?
In your parameter properties, on the Available Values screen you will see an option for the Value and the Label. This allows you to show one thing to the end user (eg: user friendly description) whilst passing another (eg: key value) to the report.
In your dataset for the test parameter add a column to hold the Label of the value you want to pass through to the query. This can be the same as your Value if you so wish, but gives you the flexibility you require on the Null entry:
SELECT DISTINCT Corp_Test as Value
,Corp_Test as Label
FROM [Tfs_Warehouse].[dbo].[CurrentWorkItemView]
WHERE ProjectNodeGUID = #ProjectGuid
AND System_WorkItemType = 'Marketing Feature'
UNION ALL -- As you have DISTINCT above, UNION ALL will work faster and give the same results as UNION (which removes duplicates)
SELECT 'Null' as Value
,'No PSI' as Label
ORDER BY Corp_Test
Once you have done this, change your parameter to use the Value field as the parameter value and the Label field as the label shown to the user.
SELECT
ISNULL(Corp_Test,'No PSI') AS Corp_Test
FROM
(SELECT DISTINCT Corp_Test
FROM [Tfs_Warehouse].[dbo].[CurrentWorkItemView]
WHERE ProjectNodeGUID = #ProjectGuid
AND System_WorkItemType = 'Marketing Feature'
UNION
SELECT 'Null'
) AS Corp
ORDER BY ISNULL(Corp_Test,'No PSI')
Remember to implement the change in your where clause main query. By main query, I mean the query that is feeding your report
The simple and easiest way is what #sgeddes answered in his comment.
I just repleced the where COALESCE(Corp_Test, 'Null') IN (#TestParam) with where COALESCE(Corp_Test, 'No PSI') IN (#TestParam), and in the dataset I replaced the SELECT 'Null' with SELECT 'No PSI'.

SSRS multiple single cells

I am trying to figure out best way to add multiple fields to a SSRS report.
Report has some plots and tablix which are populated from queries but now I have been asked to add a table with ~20 values. The problem is that I need to have them in a specific order/layout (that I cannot obtain by sorting) and they might need to have a description added above which will be static text (not from the DB).
I would like to avoid situation where I keep 20 copy of the same query which returns single cell where the only difference would be in:
WHERE myTable.partID = xxxx
Any chance I could keep a single query which takes that string like a parameter which I could specify somehow via expression or by any other means?
Not a classical SSRS parameter as I need a different one for each cell...
Or will I need to create 20 queries to fetch all those single values and then put them as separate textfields on the report?
When I've done this in the past, I build a single query that gets all the data I need with some kind of key.
For example I might have a list of captions and values, one per row, that I need to display as part of a report page. The dataset query might look something like ...
DECLARE #t TABLE(Key varchar(20), Amount float, Caption varchar(100))
INSERT INTO #t
SELECT 'TotalSales', SUM(Amount), NULL AS Amount FROM myTable WHERE CountryID = #CountryID
UNION
SELECT 'Currency', NULL, CurrencyCode FROM myCurrencyTable WHERE CountryID = #CountryID
UNION
SELECT 'Population', Population, NULL FROM myPopualtionTable WHERE CountryID = #CountryID
SELECT * FROM #t
The resulting dataset would look like this.
Key Amount Caption
'TotalSales' 12345 NULL
'Currency' NULL 'GBP'
'Population' 62.3 NULL
Lets say we call this dataset dsStuff then in each cell/textbox the xpression would simply be something like.
=LOOKUP("Population", Fields!Key.Value, Fields!Amount.Value, "dsStuff")
or
=LOOKUP("Currency", Fields!Key.Value, Fields!Caption.Value, "dsStuff")

SQL Server Sub query with multiple results

I am attempting to populate an existing bridge table from a table that exists as a template for how items are populated into the bridge table.
I am attempting to do a new insert into the bridge table (dbo.ECN_ChecklistItem) where the list of items from dbo.ECN_ChecklistItem (SELECTed by ECNID (a foreign key ID)) differs from dbo.BusinessUnit_ChecklistItem (SELECTed by BusinessUnit and InUse).
I get a SQL exception stating that "Sub query returned more than 1 value. This is not permitted when the sub query follows follows =, !=, <, <= , >, >= or when the subquery is used as an expression."
I have attempted to modify the query to include an 'in' operator rather than a '=' operator, however that was not successful either. I may have done it incorrectly.
CREATE PROCEDURE checkCurrentChecklistItemsAgainstInUseItems
--Parameters
#ECNID INT, --I need the ECNID to do the search properly.
#BU NVARCHAR(50),
#Date NVARCHAR(10), --This is the date that the checklist item was added to the ECN.
#NewStatus NVarchar(3) --This is based off of the status of the ECN, will either be '?' or 'No'
AS
BEGIN
DECLARE #System NCHAR(6)='SYSTEM'
DECLARE #Message NVARCHAR(50) = 'This value was added by the system automatically.'--Not in use.
SET NOCOUNT ON;
INSERT INTO dbo.ECN_ChecklistItem (ECNID, Required, LogBy, LogDate, Description, CheckName)
VALUES(#ECNID, #NewStatus, #System, #Date, NULL,
(Select ChecklistItem FROM dbo.BusinessUnit_ChecklistItem WHERE BusinessUnit Like #BU
EXCEPT SELECT CheckName FROM dbo.ECN_ChecklistItem WHERE ECNID Like #ECNID))
END
GO
The rest of the Insert Statement is towards a single row. However, the subquery might be returning (or has the possibility of returning) multiple rows. Whenever this situation occurs, SQL Server puts up this.
You might want to examine, by running the subquery separately as a query to find whether multiple rows are being returned even as you "assume" that there will be only row returned.
If it does indeed multiple rows, then you will need to handle that situation separately depending on your logic.
However, if you are sure only one row is being returned, you can always a MAX or MIN or TOP 1 . Although they are inconsequential when a single row is being returned, they will avoid the 'subquery returned mutliple' error.
Edit :
If you indeed want multiple rows to be inserted, replace the Values phrase, with the complete select phrase, with values for all the other fields remaining as fixed values (# ones), and the value for CheckName coming from the select statement. Select #..., #..., #...., .... CheckListItem From .... Where BusinessUnit Like ... Except... and so on.

Query hangs using parameter in WHERE clause

I am having difficulty understanding a query here.
My query ultimately returns a result set in a report where you can specify a Person in the parameter to view their data.
I am joining my Dimension table to my Employee table to return the Name from the Employee table. It looks something like this
Declare #PM varchar(30)
Set #PM = 'John Smith'
SELECT....FullName, EmployeeID, ....
FROM...
Inner Join EmployeesT on emp.EmployeeNumber = DimP.PersonID
WHERE FullName in (#PM)
Note: My Employee table is in nvarchar and Dimension is is varchar but i dont think that matters as the join still works.
Now, I set a parameter up at the top for testing.
Here is my issue: If I switch the WHERE clause to say WHERE DimP.PersonID IN ('12345') my query takes 3 seconds to run. When I change the query to WHERE FullName in (#PM) the query is taking forever to run; it hangs and runs for 5+ minuets. Has anyone experienced a similar issue?
The result set produces the correct data with multiple "people" and I want to test this by specifying a person, not an id, but when I change the parameter to the Name the query hangs..
It might be parameter sniffing. It is hanging onto the optimized execution plan for another parameter value.
Try using OPTION (OPTIMIZE FOR (#parm UNKNOWN)) at the end of your query.
Declare #PM varchar(30)
Set #PM = 'John Smith'
SELECT....FullName, EmployeeID, ....
FROM...
Inner Join EmployeesT on emp.EmployeeNumber = DimP.PersonID
WHERE FullName in (#PM)
OPTION (OPTIMIZE FOR (#PM UNKNOWN))
My coworker was able to solve this : she used `WHERE emp.empNumber = (CASE WHEN emp.FullName IN (#PM) )
:)

Select All as default value for Multivalue parameter

I'm building a report in Visual Studio 2008 with a lot of multivalue parameters and it's working great, but I would like to have have the "(Select all)" option as the default value when the report is opened.
Is there some kind of expression or SQL code I can use to make this happen? Or do I need to choose "(Select all)" every time, in every parameter, each time I want to run the report?
Try setting the parameters' "default value" to use the same query as the "available values". In effect it provides every single "available value" as a "default value" and the "Select All" option is automatically checked.
Using dataset with default values is one way, but you must use query for Available values and for Default Values, if values are hard coded in Available values tab, then you must define default values as expressions. Pictures should explain everything
Create Parameter (if not automaticly created)
Define values - wrong way example
Define values - correct way example
Set default values - you must define all default values reflecting available values to make "Select All" by default, if you won't define all only those defined will be selected by default.
The Result
One picture for Data type: Int
Does not work if you have nulls.
You can get around this by modifying your select statement to plop something into nulls:
phonenumber = CASE
WHEN (isnull(phonenumber, '')='') THEN '(blank)'
ELSE phonenumber
END
The accepted answer is correct, but not complete.
In order for Select All to be the default option, the Available Values dataset must contain at least 2 columns: value and label. They can return the same data, but their names have to be different. The Default Values dataset will then use value column and then Select All will be the default value. If the dataset returns only 1 column, only the last record's value will be selected in the drop down of the parameter.
Adding to the answer from E_8.
This does not work if you have empty strings.
You can get around this by modifying your select statement in SQL or modifying your query in the SSRS dataset.
Select distinct phonenumber
from YourTable
where phonenumber <> ''
Order by Phonenumber
It works better
CREATE TABLE [dbo].[T_Status](
[Status] [nvarchar](20) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[T_Status] ([Status]) VALUES (N'Active')
GO
INSERT [dbo].[T_Status] ([Status]) VALUES (N'notActive')
GO
INSERT [dbo].[T_Status] ([Status]) VALUES (N'Active')
GO
DECLARE #GetStatus nvarchar(20) = null
--DECLARE #GetStatus nvarchar(20) = 'Active'
SELECT [Status]
FROM [T_Status]
WHERE [Status] = CASE WHEN (isnull(#GetStatus, '')='') THEN [Status]
ELSE #GetStatus END
This is rather easy to achieve by making a dataset with a text-query like this:
SELECT 'Item1'
UNION
SELECT 'Item2'
UNION
SELECT 'Item3'
UNION
SELECT 'Item4'
UNION
SELECT 'ItemN'
The query should return all items that can be selected.

Resources