Are SSRS parameters fixed while generating a report? - reporting-services

I have been using the following code within my SQL queries:
DECLARE #CurrentDate DateTime = Getdate()
I then reference #CurrentDate instead of using the Getdate() function multiple times within the same query. This is useful because the value of #CurrentDate doesn't change during execution of the query.
However, I have started converting some of these variables to parameters set by SSRS. This allows the user to change the parameter #CurrentDate which is useful for various reasons. Typically the default value is set to "=Now()" or some expression containing the Now() function.
At what stage are these parameters calculated and is there a "correct" way to calculate them if I want parameters to be consistent with one another?
I'm trying to figure out if I should have one parameter for the current date and then reference that in the calculation of my other parameters or if this will produce the same inconsistencies (or worse inconsistencies) as simply using Now() within the expression for each parameter.

It all depends on what you mean by "consistent". Take these two scripts for example:
--Method #1
UPDATE tracker.shipment SET delivery_date = GETDATE() WHERE delivery_id = 1;
WAITFOR DELAY '00:00:01';
UPDATE tracker.shipment SET delivery_date = GETDATE() WHERE delivery_id = 2;
--Method #2
DECLARE #current_date DATETIME = GETDATE();
UPDATE tracker.shipment SET delivery_date = #current_date WHERE delivery_id = 1;
WAITFOR DELAY '00:00:01';
UPDATE tracker.shipment SET delivery_date = #current_date WHERE delivery_id = 2;
This is a pseudo script to update a tracking system to show when delivery was made. I would argue that the first version is more "consistent" as it records when the update was actually made, and not an arbitrary date/ time from when the stored procedure was first entered.
Let's pretend that there's another system constantly checking for delivery items that have a delivery date added, then makes the actual delivery, or that the code immediately before each delivery update does something to trigger the actual delivery. In this case it would be much less consistent to use the second method, as this would record deliveries as being made for some time in the past, before they actually were delivered.
As far as I am aware the parameter sent by SSRS will be evaluated to determine the server time on the reporting server at the point where the user clicks the "View Report" button. So every parameter that has Now() will end up with the same date time, but this might be slightly out from the SQL Server if your servers aren't exactly in synch.
Does this actually matter? Well that depends entirely on what you are doing with the date/ time you are passed I guess. When your users enter a custom date/ time are they entering a date or a full date/ time? I imagine they only enter a date, so this is automatically converted to having a 00:00:00.00 time component?
Basically I would need more context to be able to give a fuller answer to this.

Just create a datetime SSRS parameter and set the default value to NOW(). Then it will assume everything, but users can opt for a different date. Note also that SSRS uses DATETIME parameters, but for a lot of my reports I actually use a string parameter field which I CAST to DATE to avoid losing data between hours that the user isn't expecing:
WHERE CAST(StartDate AS DATE) > CAST(#StartDateParam AS DATE)

Related

MS Access db, VBA form dynamic Default value on condition

I'm using form below and not sure if I can implement this logic:
Start Date = Now();
If Expedited then Exp_Date = Start_Date + 72 hours
else Exp_Date = Start_Date + 30 days.
I used this expression for default but looks like at time of evaluations it doen'st care about value in ExpeditedYN.
=IIf([ExpYN]=True,DateAdd("h",72,Now()),DateAdd("d",30,Date()))
Ideally I'd like to flip Exp.Date default clicking on Expedited box, is this available in VB. Tried DLookUp (D --> Dynamic??) and it doesn't work
DLookUp(IIf([ExpYN]=True,DateAdd("h",72,Now()),DateAdd("d",30,Date())))
Tx
Dai
Using IIf or DLookup to refer to another field on the same record in a Default Value isn't going to get you anywhere, because Access only asks Default Value what value to fill in when the new record is first initiated. That means it's going to be based on the default values of the other fields, and therefore will not respond to any user input.
I'd suggest that you keep the Default Value of both StartDate and DeadlineDate simple, similar to how you already are:
StartDate's Default Value: Now()
DeadlineDate's Default Value: DateAdd("d",30,Now())
And then change it in code if and when Expedited is changed. There's a few ways of doing this, I'd probably put it in the Before Update event of the checkbox, so it runs without having to move out of the record. You could also base it on the StartDate field if you might want to be able to change the record later on. You could build it with Access' macro builder, but it's easier to show you VBA here:
If ExpYN Then
DeadlineDate = DateAdd("h", 72, StartDate)
Else
DeadlineDate = DateAdd("d", 30, StartDate)
End If
If you want the expedition process to be based on the moment the button is clicked instead of the DateTime data in StartDate, just change StartDate (or whatever that field is called) to Now().
DefaultValue triggers only once - when record is first initiated by input into any field. If the calculation is based on value of Expedited, the DLookup should not work because the record is not yet committed to table. Record is committed when: 1. close table/query/form or 2. move to another record or 3. run code to save. If you want to programmatically modify DeadlineDate value based on Expedited value, then code needs to be in Expedited AfterUpdate event and abandon the DefaultValue approach.
Me!DeadlineDate = IIf(Me!ExpYN, DateAdd("h",72,Now()), DateAdd("d",30,Now()))

sql query to get a scheduled report to run for that curent month only

I have an SSRS 2005 report that has a dataset for to an Oracle database. The report that i have essentially just pulls all data back from an audit log. This report works perfectly fine and i have scheduled thi to run and send an email using the new subscription method within SSRS.
The only issue i have is i want the report to run on the last day of the month (its set up to do this alreday) and run the report based on that month only. Getting the report to run specifically for a month is what im unsure on? I though i would be able to set parameters within the report but these only enable my user to select a calendar date?
Is there a DATEADD or DATEPART type of function i can use to complete this task?
Regards
B
Best bet would be to set up a dynamic date default for the date patameter at the report level, and set up the subscription to use this default value, so whenever the subscription runs it would automatically have the appropriate date.
Probably the easiest thing to do is set the parameter as the current date, if that would work, e.g. something like =Today(). So that will be calculated whenever the subscription runs.
You can set the default value for whatever you need; first day of the current month, etc. You should able to set whatever value you need without too much trouble.
So for the first day of the month something like:
=DateSerial(Year(Today()), Month(Today()), 1).
Users running the report ad-hoc could just reset the parameter as required.
I'm assuming the report handles the amount of data to return correctly assuming the right data parameter is sent.
2 Parameters #DateFrom and #DateTo
Default Value for #DateFrom that outputs the first day of the current month:
=DateAdd("d",-Day(Today())+1,Today())
Default Value for #DateTo that outputs the last day of the current month:
=DateAdd("d",-Day(Today())+1,DateAdd("m",1,Today()))

How do I retry until a given date matches current date within For Loop Container?

Scenario:
I have a lookup table which has a date column, I need to look at this date column and check if its today's date, if not then wait for 5 mins and check the same thing again, and if the date is current send an email and exit the loop, and if after 6 retries if the date is not current execute a SQL task.
I have a ForLoop Container with the following settings:
InitExpression : #[User::Counter] = 0
EvalExpression : #[User::Counter] < 6
AssignExpression : #[User::Counter] = #[User::Counter] + 1
How / Where do I check the date :
SELECT ControlTimeStamp from LOOKUPTABLE
WHERE ControlTimeStamp = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
Note:
I'm using Business Intelligence Development Studio (BIDS) 2008 for SSIS package development.
I think you'll want an approach like this. Execute your SQL Task to determine whether today is your date. From there you'll need to either sleep for N minutes or you'll want to send an email. The trick is to use an Expression on the Precedence Constraint between the Execute SQL Task the children.
My implementation differs slightly from what yours but the concept remains the same. I created two variables, #ActualDate and #ReferenceDate. #ActualDate is today and #ReferenceDate gets set from the Execute SQL Task. I then see whether they are equivalent. For what you have, if you get a result, then you know the send mail condition has been met so change your Expressions to meet that.
What isn't shown is how to terminate the loop early as I'm not quite certain how to do that.

Fields cannot be used in report parameter expression

I have to set the start_date of my report depending of a report parameter. The time stamps are calculated in a database query.
My expression looks like this:
=SWITCH (
Parameters!report_type.Value = 1,First(Fields!daily_start.Value, "Timestamps")
,Parameters!report_type.Value = 2,First(Fields!weekly_start.Value, "Timestamps")
,Parameters!report_type.Value = 3,First(Fields!monthly_start.Value, "Timestamps")
)
Unfortunately I get the error message:
A value expression used for the report parameter 'time_from' refers to a field. Fields cannot be used in report parameter expression
I know, that this is not allowed because SSRS cannot be sure in which order datasets are called. But I think this is not dangerous.
All time stamps are received by query without parameter. The parameter report_type is selected by a user before the report will be generated.
Can someone give me a hint for a workaround?
Here's the workaround - get the value using SQL.
Create a new Dataset called StartDates:
SELECT CASE
WHEN #report_type = 1 THEN daily_start
WHEN #report_type = 2 THEN weekly_start
WHEN #report_type = 3 THEN monthly_start
END AS StartDate
FROM MyTable
You already have the #report_type and #time_from parameters. With the #time_from parameter, set its Default Values to Get values from a query using the StartDates dataset and the Value field StartDate.
Now, you'd think this might be enough to make it work - you're referencing this query as the default value and as you change the #report_type parameter the other parameters refresh, but the first date in the #time_from parameter never changes. That's because the refresh happens on the Available Values query, not on the Default Values query.
So you also need to wire up the Available Values query to the StartDates query. Now your query will fire on the change of #report_type and the default value will be set to the appropriate date for your selection.
I switched from a query to Stored Procedure and was getting this error. Things I tried:
Ensured I had sufficient permission on the database (you need EXEC rights or DBO to run teh sproc)
Delete the existing parameters (and then use refresh fields to refresh/get the correctly named ones back)
Remove the square brackets around the stored procedure if you've specified that
Sometimes, Expressions can get a bit verbose. I have created a Report Code Function and then used that as the Parameter Value.
For example, I created a Code function called "CalculateDateSet" and then set the Report Parameter to this expression:
"=Code.CalculateDateSet(Parameters!Month.Value, Parameters!Year.Value"

SSRS 2008R2 Report parameter with Now() default not auto-refreshing

System - SSRS 2008R2 using report builder 3.0 (or BIDS if you think the answer lies there)
Objective - Create a report that displays sales data from the past 24 hours for each sales region. The report needs to refresh every 10 minutes, and always show the last 24 hours from the last refresh. However - the users still need to have the ability to change the time frame.
What did I do?
Built a report that shows sales data for a single region depending on 3 parameters: StartTime, EndTime, RegionCode. Lets call it RegionalSales
Built a master report that includes multiple subreports of RegionalSales. the master report has 2 parameters - StartTime (with a default value dateadd("h",-24,Now())) and EndTime (with a default value Now()). I am sending StartTime and EndTime to the subreports, and every subreport is assigned it's own region.
I've set the Auto-Refresh property of the master report to 600.
Results -
At first everything seems perfect - the master report loads, and the data shown is correct for the loading time (let's say 22:08). However, after the auto-refresh (that occures at 22:18) the data is still correct as to 22:08.
The problem is that on the first load of the report the parameters get their default values as I stated (StartTime = 10/12/2011 22:08 & EndTime = 11/12/2011 22:08).
However, on the auto-refresh instead of the paramters updating (So I'll get StartTime = 10/12/2011 22:18 & EndTime = 11/12/2011 22:18) I still get the old values (StartTime = 10/12/2011 22:08 & EndTime = 11/12/2011 22:08)
Any thought on why, and how can I change this?
Thanks!
If I understand correctly...
Unfortunately, report parameters get their values only at the first load unless the user manually clicks the View Report button.
To get around this, I'd try converting the date parameters to not use default values, but rather get their values from a query and use the equivalent of GETDATE() for your RDBMS. If I'm not mistaken this will require you to add another DataSet to your report... but it won't really be a dataset since the query for it will simply be SELECT GETDATE() [or the equivalent for your RDBMS] and whatever transformations you need to do for that.