Email schedule that runs in the background in ASP.NET MVC - sql-server

How to create an email that run in the background as long as there is a timeout > 2 between 2 dates.
2 days the difference between the 2 dates otherwise there is a time overrun as in the photo (opening date1, closing date2 : date2 -date1 > 2 => mail)

You can follow below steps:
Create a background SQLAgent job, which runs on regular schedule, say daily once or every 12 hours, based on the notification schedule.
This SQLAgent job, polls the schedule table and sends email using sp_send_dbmail.
Use DATEDIFF(day,GETDATE(),TravelDate) < 2 to find out the travels within 2 days.
send email using sp_send_dbmail

Related

SQL Server : how to automatically update data in table after a certain time interval

I have an OrderProduct table with these columns and some data:
-order_number : ORDER01
-customer_name : Jackie
-order_status : Wait For Payment
-datetime_order_status : 25-01-2020 15:30:00
-datetime_transfer_notify : NULL
A customer needs to transfer notify in my order product system in 24 hours if not the Microsoft SQL will automatic update data in column 'order_status' from 'Wait for payment' to 'Cancel'.
How can I do that?
I believe the easiest way to do this is with a SQL Agent job (MS Docs). This is very dependent on the architecture and size of your databases and tables, but it would definitely get the job done. Depending on how sensitive the business is to being up to date, you could set the job to run every 1 minute, every 5 minutes, or any other time interval you would like. If I was going to do this, I would use a query along the lines of the following:
UPDATE OrderProduct SET order_state = 'Cancel' WHERE datetime_order_status < DATEADD(DAY, -1, GETDATE()) AND order_status = 'Wait for Payment'
Along with this, I would use something like SQL Server Management Studio to create a SQL Agent job on that server that ran at the interval you'd like, similar to this (Stack Overflow). Here (Stack Exchange DBA) is a very similar question to yours for MySQL as added reference.

How to set SSRS subscription time as an expression

I have a report and the report StartDate and EndDate parameters are using the expression as a default value below.
=DateAdd(DateInterval.Minute,0,DateAdd("h",7,DateAdd("h",-24,Today())))
=DateAdd(DateInterval.Minute,0,DateAdd("h",7,Today()))
When I execute the report, the report is starting from the day before at 7 AM to today 7 AM.
I would like to keep the report Start time and End time like this(07:00).
I also want to send the report to customer every day 7:30 AM but the report needs to be executed according to start date and end date paramaters.
Example: today 12.12.2019
Subscription time will be 07:30 AM
report needs to be running this time:
StartDate : 11/12/2019 07:00:00
EndDate : 12/12/2019 07:00:00
But when I schedule subscription every day and 7:30 AM, I received report from one day before 7:30 AM and today 7:30 AM.
I just want to see report from 7:00am to 7 am. Even if I change schedule time.
Could you please help me about this problem. How can I edit my subscription?
Is it possible to write an expression in "date/time from - date/time to" fields in subscription?
Btw, When I unclick “use Default” part, it always takes 11-12-2019 even 2 days after ☹
Time from needs to be one day before at 07:00 AM
Time to should be on that day at 07:00 AM
Do you have any suggestion for it?
Thanks
I resolved my issue. There are 2 solutions for it.
Option 1 :
In the report design If it is must to have those date parameters must be DATTEIME and to allow TIME factor as well then and if you want to run the report which is subscribed always for Yesterday 7:00 to today 7:00 am then I would not rely on sending any parameter values based on expressions …I would set up Date/Time Parameter in report design to allow null values and send null values as default from the subscription settings.
Then In report SP you can always add a clause at the TOP like
if #startDateTime is null AND #endDateTime is null
begin
set #startDateTime =CONVERT(VARCHAR(10), getdate(), 111);
set #startDateTime =dateadd(hh,7,( dateadd(d,-1,#startDateTime)))
set #endDateTime =dateadd(d,1,#startDateTime)
end
and let the rest SP be same
Option 2 :
If you can change the report parameters to be a type only DATE then its easy always send =Today() in your subscription parameter for both Start & End
Then In report SP you can always add a clause at the TOP like
if #startDateTime = #endDateTime
begin
set #endDateTime =CONVERT(VARCHAR(10), #endDateTime, 111);
set #endDateTime =dateadd(hh,7,#endDateTime)
set #startDateTime =dateadd(d,-1,#startDateTime)
end
and let the rest SP be same
Option 2 is better if they are ok to have Start & End date parameter as just DATE instead of DATETIME.
Any way Using any of these options do handle this in SP… you can always have control in future if they want to change time form 7:00 am to any other time …no need to change report design just update SP…2 minutes
You can schedule this report for any time of the day and it will always send them a report for Yesterday 7:00 to Today 7:00

get the last modifed user for a sql job agent job

The schedule for one of my job agents jobs has recently been disabled, is there anyway to find out any information other than last modified date on who or what disabled the job schedule?
SQL Server doesn't audit this information by default, so no, this data is not going to be available to you after the fact. (I checked the default trace and it doesn't seem to be logged there.) If you haven't since re-enabled the job, you may be able to correlate the value in msdb.dbo.sysjobs.modified_date with other information that is logged, but I have no idea what other events you might be able to ascertain belong to the same user as the one who modified the job. Again, if the job hasn't already been modified (or you know when it was modified before you fixed it), and assuming the change happened in the timeframe that is still within your current rolling window for the default trace, you can check for other activity around the same time:
DECLARE #ModifiedDate DATETIME;
SET #ModifiedDate = -- plug in the value here
DECLARE #path NVARCHAR(260);
SELECT #path = REVERSE(SUBSTRING(REVERSE([path]),
CHARINDEX(CHAR(92), REVERSE([path])), 260)) + N'log.trc'
FROM sys.traces WHERE is_default = 1;
SELECT * FROM sys.fn_trace_gettable(#path, DEFAULT)
WHERE EndTime >= DATEADD(MINUTE, -30, #ModifiedDate)
AND EndTime < DATEADD(MINUTE, 30, #ModifiedDate)
ORDER BY EndTime DESC;
You could set up your own server-side trace, extended events session, event notification or audit to make sure that you are able to audit this information in the future (or simply restrict the ability of your whole team to mess with jobs).
The easy part is to find what jobs and/or schedules that have been disabled recently.
-- Use msdb
use msdb
go
-- Jobs that have been recently disabled
select
[name], [enabled], [date_created], [date_modified]
from sysjobs
where [date_modified] > '2013-09-30' and enabled = 0
order by [date_modified] desc
go
-- Schedules that have been recently disabled
select
[name], [enabled], [date_created], [date_modified]
from sysschedules
where [date_modified] > '2013-09-30' and enabled = 0
order by [date_modified] desc
go
Unless the user or sysadmin took ownership of the job, it is hard to find out who did it.
Things that I thought of but did not work were the following.
1 - Any entries in the server or agent logs? NO DICE
2 - Does the default server side trace pickup the event? NO DICE
3 - Can I look at the transaction log to find the person? MSDB uses a simple recovery model. NO DICE
4 - Does the health check (extended events) track this information. Which I doubted, but wanted to check. NO DICE
5 - Since this is not an error, nothing gets logged in windows events. NO DICE
Therefore, after a couple google searches, I think you are down to a couple solutions.
A - Create a trigger on the appropriate system table and save audit information.
See my blog on 'How to audit and prevent unwanted user actions.'. It is a full blown presentation that I do at SQL Saturdays.
B - Create a http://www.bimonkey.com/2009/12/sql-server-2008-auditing/ audit specification but you will have to read the output file.
C - Add a server side trace or extended event to capture the data.
Good luck.
John

Execute the script itself in every 5 min by getting the system time

I want to write a script that will execute in every 5 min, and based on that certain actons will perform.
To be more clearify we take a simple example of inserting tha data in the table:
Insert into EmpDetails(id, Name, Designation) values (1, 'ABC', 'Developer')
I just want to insert the same data in every 5 min according to my system time.
Can anyone please suggest me. Thanks in advance.
The BEST solution is running using SQL Agent Jobs
Expand the SQL Server Agent node and right click the Jobs node in SQL Server Agent and select 'New Job'
In the 'New Job' window enter the name of the job and a description on the 'General' tab.
Select 'Steps' on the left hand side of the window and click 'New' at the bottom.
In the 'Steps' window enter a step name and select the database you want the query to run against.
Paste in the T-SQL command you want to run into the Command window and click 'OK'.
Click on the 'Schedule' menu on the left of the New Job window and enter the schedule information (e.g. daily and a time).
Click 'OK' - and that should be it.
(There are of course other options you can add - but I would say that is the bare minimum you need to get a job set up and scheduled)
Pulled from Previous SO Answer
Use the SQL Jobs and shcedule for 5 minutes . In case you have the feature enable.
You can do this with a script. After creating the job in the agent job section, or via script, this can all be automated.
EXEC msdb.dbo.sp_add_schedule
#schedule_name = 'Your_schedule_name',
#freq_type=4, -- daily
#freq_interval = 1, -- every day
#freq_subday_type=4, -- Minutes
#freq_subday_interval = 5 -- every 5 minutes
Then attach the schedule
EXEC msdb.dbo.sp_attach_schedule
#job_name = N'Your_job_name',
#schedule_name = N'Your_schedule_name' ;

Problem in Getting the DateTime Variable from the Database

I used to get the Login Time of a user when he Logsin at the First Time to the Syatem, all the activities the user is doing will be stored under a seperate column called 'Activity'. My Problem is the User can LogOn to the System many times in a Particular Day, but i want to retrieve only the First Logon Time. The LogOn timings are appearing when i write a query for getting the Timings based on Activity are Like below.
Time User Activity
'3/23/2011 9:55:00AM' kk LogOn
'3/23/2011 5:30:00PM' kk LogOn
I write a Query to retrieve these values like this
Select StartDateTime,User,Activity from AgentLog where Activity='LogOn'. So, any one can give me Small help that how can i retrieve only the First LogOn time for the User given. I am Using Sql Server 2008, There will be Somany days, the User Logging On, so for every day i need to get the Logon Time
Get the earliest time for each day they logged in by using the following grouping:
Select min(StartDateTime)
from AgentLog
where Activity='LogOn' and
User = 'Username'
group by datepart(day, StartDateTime),
datepart(month, StartDateTime),
datepart(year, StartDateTime)
If you won't store times as a string but as a timestamp (in seconds) it would be easy to just select the data:
Select StartDateTime,Activity from AgentLog where Activity='LogOn' AND time>timeofday ORDER by time LIMIT 1
Where timeofday is the timestamp of the current day at midnight

Resources