SSRS - Specifying Values in Parameter Expression - reporting-services

Good afternoon -
I am working Visual Studio 2013, attempting to build one (1) parameter which has different values assigned to each option. I have created a data attribute which contains the following values:
Active
Error 1
Error 2
I have specified the value in an expression for each option:
All = "Active", "Error 1", "Error 2"
Active = "Active"
Errors Only = "Error 1", "Error 2"
The only option that works is the Active option. The others return no records.
How should I define the expression?
Any and All assistance is very much appreciated!

here is what you can do, you need to handle the query condition formatting and parameter values correctly. e.g. see below example.
Dataset Query for Stored Procedure:
*declare #renderType nvarchar(max)
declare #script nvarchar(max)
--set #renderType = 'txt,dump'
SELECT #renderType = REPLACE(#renderType , ',', ''',''')
set #script = 'select render_format, subscriber_id from Sub_fact where render_format in (''' +#renderType+ ''')'
exec (#script);*
create stored procedure to do all this formatting correctly.
now in parameter properties this is what i have provided
this works perfectly for me.
change your solution accordingly and it should work. respond with how it goes.

Related

SQL Openquery - Object has no columns

I have a dynamically Stored Procedure, which creates an mdx statement for an OpenQuery. So it can happen that the objects from the mdx statement are empty. In this case I want back an empty string.
Generally, the query works except when I choose a date that is from the future in which case the SQL Server gives me this error:
"The OLE DB provider "XYZ" for linked server "XYZ" indicates that
either the object has no columns or the current user does not have
permissions on that object."
select
t.*
from OPENQUERY([SomeServer_OLAP],''
SELECT
non empty{[Measures].[FactWorkItemHistory Microsoft_VSTS_Scheduling_OriginalEstimate],
[Measures].[FactWorkItemHistory Microsoft_VSTS_Scheduling_CompletedWork],
[Measures].[Microsoft_VSTS_Scheduling_RemainingWork]} ON COLUMNS
, NON EMPTY { ([Work Item].[Iteration Path].[Iteration Path].ALLMEMBERS
* [Work Item].[System_AssignedTo].[System_AssignedTo].ALLMEMBERS)} on ROWS
FROM [Team System]
WHERE '+#Month+'
'') t'
So, entering the date parameter for December (the month of writing this post) works fine, but entering January of 2018 (next month) and all the other following months returns the error. Any help is appreciated.
Removing "non empty" from the query fixed my issue. There doesn't seems to be any immediate observable drawbacks to this.

dynamic queries in SSRS

I would like to write a query and allow for a grouping on different colums; for example the end user can group either on country, region, city, or no grouping.
The result is to be used in an ssrs report.
so I would write a query like this:
#value = CASE WHEN #dropdown=1 THEN ', foo.country' ELSE ', foo.region'
#sqlquery = 'select name '+#value+' from foo group by field1'+ #value + ';'
EXEC(#sqlquery);
This does work as expected in management studio (if i define variables and assign something to #dropdown);
I'm a bit at loss on how to implement this in an ssrs report: I want the user to be able to choose no grouping (leave the #value empty)
Visual studio will not be able to 'see' the field and therefore allow to add it in a tablix
Avoid users type the grouping (see the #NeilP recommendation), just create a parameter called Grouping, in Available Values tab you can specify the dropdown selector values.
In the Available Values tab use these settings:
Then in your dataset properties map the SSRS parameter to a T-SQL parameter.
Now you can use #Grouping in your dataset:
set #value = CASE WHEN #Grouping = 1 THEN ', foo.country'
WHEN #Grouping = 2 THEN ', foo.region'
WHEN #Grouping = 3 THEN ', foo.city'
ELSE ''
END
set #sqlquery = 'select name '+#value+' from foo group by field1'+ #value + ';'
The user will be prompt to select one value in the dropdown list:
You can define a default value used in the report, which lets users run the report without specify any value.
Go to Default Values tab in Parameter Properties and add =0 if you want to your report runs without any group by default.
Let me know if this helps.
Try this:
1. Create a parameter GroupBy
Create your data set. Don't directly write your query in to box, use expression instead.
Write your query like this:
I think you probably need to try and adjust your query expression for a while, but it should be the way to go. Good luck.
I would wrap this in a stored procedure and then call that from the dataset
create proc report_1
#value varchar(200)
as
#sqlquery = 'select name '+#value+' from foo group by field1'+ #value + ';'
EXEC(#sqlquery);
Be weary of SQL injection when using dynamic sql, it might be advisable to set a defined list of values for your drop down parameter, either statically or by querying the information schema for column names.

Table valued parameters for SSRS 2008

We have a requirement of generating SSRS reports from where we need to convert multi-valued string and integer parameters to datatable and pass it to stored procedure. The stored procedure contains multiple table type parameters. Earlier we used varchar(8000) but it was also crossing the datatype limit. Then we thought to introducing datatable concept. But we were not aware of how to pass values from SSRS.
We found a solution from GruffCode on Using Table-Valued Parameters With SQL Server Reporting Services.
The solution solved my problem, and we're able to generate reports. However, sometimes SSRS returns the two following errors:
An error has occurred during report processing.
Query execution failed for dataset 'DSOutput'.
String or binary data would be truncated. The statement has been terminated.
And
An unexpected error occurred in Report Processing.
Exception of type 'System.OutOfMemoryException' was thrown.
I'm not sure when and where it's causing the issue.
The approach outlined in that blog post relies on building an enormous string in memory in order to load all of the selected parameter values into the table-valued parameter instance. If you are selecting a very large number of values to pass into the query I could see it potentially causing the 'System.OutOfMemoryException' while trying to build the string containing the insert statements that will load the parameter.
As for the 'string or binary data would be truncated' error that sounds like it's originating within the query or stored procedure that the report is using to gather its data. Without seeing what that t-sql looks like I couldn't say why that's happening, but I'd guess that it's also somehow related to selecting a very large number of parameter values.
Unfortunately I'm not sure that there's a workaround for this, other than trying to see if you could figure out a way to select fewer parameter values. Here's a couple of rough ideas:
If you have a situation where users might select a handful of parameter values or all parameter values then you could have the query simply take a very simple boolean value indicating that all values were selected rather than making the report send all of the values in through a parameter.
You could also consider "zooming out" of your parameter values a bit and grouping them together somehow if they lend themselves to that. That way users would be selecting from a smaller number of parameter values that represent a group of the individual values all rolled up.
I'm not a fan of using a Text parameter and EXEC in the SQL statement like the article you referenced describes as doing so is subject to SQL injection. The default SSRS behavior with a Multi-value parameter substitutes a comma-separated list of the values directly in place of the parameter when the query is sent to the SQL server. That works great for simple IN queries, but can be undesirable elsewhere. This behavior can be bypassed by setting the Parameter Value on the DataSet to an expression of =Join(Parameters!CustomerIDs.Value, ", "). Once you have done that you can get a table variable loaded by using the following SQL:
DECLARE #CustomerIDsTable TABLE (CustomerID int NOT NULL PRIMARY KEY)
INSERT INTO #CustomerIDsTable (CustomerID)
SELECT DISTINCT TextNodes.Node.value(N'.', N'int') AS CustomerID
FROM (
SELECT CONVERT(XML, N'<A>' + COALESCE(N'<e>' + REPLACE(#CustomerIDs, N',', N'</e><e>') + N'</e>', '') + N'</A>') AS pNode
) AS xmlDocs
CROSS APPLY pNode.nodes(N'/A/e') AS TextNodes(Node)
-- Do whatever with the resulting table variable, i.e.,
EXEC rpt_CustomerTransactionSummary #StartDate, #EndDate, #CustomerIDsTable
If using text instead of integers then a couple of lines get changed like so:
DECLARE #CustomerIDsTable TABLE (CustomerID nvarchar(MAX) NOT NULL PRIMARY KEY)
INSERT INTO #CustomerIDsTable (CustomerID)
SELECT DISTINCT TextNodes.Node.value(N'.', N'nvarchar(MAX)') AS CustomerID
FROM (
SELECT CONVERT(XML, N'<A>' + COALESCE(N'<e>' + REPLACE(#CustomerIDs, N',', N'</e><e>') + N'</e>', '') + N'</A>') AS pNode
) AS xmlDocs
CROSS APPLY pNode.nodes(N'/A/e') AS TextNodes(Node)
-- Do whatever with the resulting table variable, i.e.,
EXEC rpt_CustomerTransactionSummary #StartDate, #EndDate, #CustomerIDsTable
This approach also works well for handling user-entered strings of comma-separated items.

Parameters in SQL Server 2008

I have a stored procedure that pulls data for a report. I'm having a problem with the parameters. I have a couple temp tables and some joins that work so I have omitted them below. The problem is this line:
WHERE
SeminarDivision = #SeminarDivision AND SeminarType = #SeminarType
When I put this where clause in to use my seminar parameters the stored proc returns nothing But I need to generate a report based on those two parameters. So where do the parameters go? Can anyone help?
#StartDate DateTime,
#EndDate DateTime,
#SeminarDivision VARCHAR(50),
#SeminarType VARCHAR(50)
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
... OMITTED
SELECT
WL.PID,
CONVERT(varchar(20), upper(substring(FirstName,1,1))+
LOWER(substring(FirstName,2,19))) AS FirstName,
CONVERT(varchar(20), upper(substring(LastName,1,1))+
LOWER(substring(LastName,2,19))) AS LastName,
S.SeminarDivision,
S.SeminarType,
S.StartDate,
S.SeminarLocation
FROM
#tblWaitList WL
INNER JOIN #tblSeminar S ON WL.SeminarGuid=S.SeminarGuid
WHERE
SeminarDivision = #SeminarDivision AND SeminarType = #SeminarType
ORDER BY
LastName,FirstName,StartDate
First and foremost there is nothing wrong with your code, when asking where do these parameters go, they go exactly where you put them. The question is - is the data coming in for SeminarDivision and SeminarType the right type of data? For instance just as a test,
copy the code into a new sql code query inside the editor. Run the command without the where, if you get values great. Now change the where to
WHERE
SeminarDivision = "Possible_Value"
Where Possible_Value should be a possible value...If it returns rows, good...now add the second condition also hardcoding a value:
WHERE SeminarDivision = "Possble_Value" AND SeminarType="Possible_Value_2"
Getting any data? Is it possible you want OR rather then AND ?
There's nothing wrong with the 'location' of your params.
If you're getting no data back, it's either because you've not populated #tblWaiList or #tblSeminar or because the records simply don't match your WHERE clause.
Check your params have the value you think they do by executing print #SeminarDivision etc.
SELECT * FROM #tblSeminar may give you a clue too.
You are not setting parameters correctly for the call.
Try this in SSMS, change values accordingly
EXEC Proc '20110101', '20111101', 'PossibleDivision', 'PossibleType'
If this fails, then show us "OMITTED" code
if this works, show us how you are calling this from the client code

SSIS SQL Task - "Parameter name is unrecognized"

I have a SQL Task that needs to run a simple update to update a single row.
I have set the SQLStatement to:
update agency set AgencyLastBatchSeqNo = ? where agencyID = ?
On the Parameter Mapping page I gave set Parameter 0 and Parameter 1 to variables that I know contain the right values. I have also set the Parameter Name values correctly.
In the database, the column AgencyLastBatchSeqNo is an int, AgencyID is a big int. Does anyone have a reference to find what the data types map to in SSIS? I have guessed at SHORT for the int and LONG for the big int.
When I run the task I get the following error:
[Execute SQL Task] Error: Executing the query "update agency set AgencyLastBatchSeqNo = ? where AgencyID = ?" failed with the following
error: "Parameter name is unrecognized.". Possible failure reasons:
Problems with the query, "ResultSet" property not set correctly,
parameters not set correctly, or connection not established
correctly.
Could anyone please suggest what may be wrong?
Thanks
Rob.
The answer to this is to change the Parameter Name value in the Parameter Mapping screen.
Given the following query
SELECT Id, AnimalName FROM dbo.Farm WHERE Farm_id = ?
Assuming my Parameter is an integer Variable named User::Farm_id
Choose the following values on the Parameter Mapping Screen
Variable Name - User::Farm_id
Direction - Input
Data Type - LONG
Parameter Name - 0
Parameter Size - -1
Originally the Parameter Name will be "NewParameterName". Simply change this to the ordinal position of your variable marker ("?")
If you are using more than 1 parameter then in the execute sql task window go to parameter mapping and set the parameter name to 0,1,2,3....depending on the number of parameter and the parameter size to -1..
This must be helpful to resolve your issue.
One thing you don't mention is your connection type. I assume you are not using ADO.Net since the parameter marking in that case is not a ?. For the other types of connection, parameters are named as follows:
ADO (not ADO.Net) connection: parameter names are Param1, Param2...
ODBC connection: parameter names are 1,2,3...
OLEDB connection: parameter names are 0,1,2...
For the variable types (they are different in the parameter mapping section than in any other area of SSIS) I typically use Long for Int's and I typically leave the length set to -1. I believe that a Long will work for both Int's and Bigint's.
See SSIS data types.
int = DT_I4 (4 byte integer) = Int32 variable
bigint = DT_I8 (8 byte integer) = Int64 variable
Make sure you're quoting your values, and that you don't have typos in your column names.
When defining the parameter mappings any trailing blanks after the parameter name can cause this message too.
Go to Parameter Mapping and change Mapped Parameter_Name = 0 instead of using default name.
and for next parameter it should be 1. and so on...
Note:- you have to change the "Data Type" as per the value.
it will just take place on the occurrence of "?"