Sending emails automatically using SQL Server job - sql-server

I'm developing a .NET desktop application with SQL Server as the database backend. One of the requirements of the application is that if a record status, for example, remains inactive for 30 days, there will be a reminder email sent to the user associated to that record.
This could be done pretty easily within the application, as long as it is started and running. However, assume that for a certain period of time, nobody starts up the application, the reminder email won't be sent, because nothing / nodody triggers the action.
How about creating a job in SQL Server which can monitors the records and sends emails as needed? Has anyone ever done that?
Thanks a lot!

Given the requirements of your task, I suggest that you create a console program (w/ C# or VB.NET) that checks for inactive (30 days) row condition and then generates the appropriate email notification message. Then run this program every hour or so (depending on the urgency involved in detecting an inactive row condition) using a SQL Server Agent Job.
The following image shows how the SQL Server Agent Jobs are displayed in the Object Explorer for SQL Server 2008 R2.
This SO entry covers some aspects on creating a console program that runs at certain times. The SQL Server Job Agent has several scheduling options that should facilitate your needs.
You might be reluctant to create a console program for this, but you are apt to find that doing so gives you options that are simply not easily implemented with a pure SQL Server based approach. Plus, you may have future needs that require similar processing that this approach provides.

Related

Application with SQL Server express and scheduling a backup everyday

I have an application that is used by more than one user, however backups need to be performed twice a day. I don't have SQL server agent and i was wondering if i should create an exe that would run in the background.
I have read other posts about using the scheduler, but i would look it to be away from the end user and simple exe is used. As i am looking to incorporate sending an email once back up is complete.
basically what is the best approach for creating a backup on SQL server and how does other applications allow for this?
Update
Would a good idea be to create a windows service that checks the time, if time matches then perform backup, i have never created a windows service.
What are the positives and negatives ?
will this start on pc boot?
any other suggestions?

Different users Access front end generating different SQL for backend

Here's my situation:
Both users are using the same Microsoft Access front end.
Both users have the same version of the SQL Server Native Client 10.0 ODBC driver.
Both users have the same version of Microsoft Office 2007 installed.
Both users are opening the same report with the same parameter values.
One takes 2 minutes, the other takes an hour.
The issue travels with the user, it happens regardless of which PC they log into on the network.
I checked the SQL passed to SQL Server and it is different. The one that runs quickly is standard T-SQL. The one that hangs is sending a parameterized query that has a terrible execution plan. I can't figure out what is causing it to send one query for one user and a different query for another user.
Since the issue travels with the user regardless of which PC they are on. This makes me think that it is some sort of obscure group policy setting or active directory setting that is part of the users account that is being replicated from PC to PC. Which probably means it's in the registry somewhere. But I am having a difficult time tracking down what that registry key is. Does anyone have any ideas?

Win Form Application Runs a Method Everyday Even If It is Not Running

I have a VB .Net Windows Form application with MS SQL server for database part of it. I need to run a method which essentially depending on some date sensitive data in the database may or may not create a notification email to be sent to one or more recipients. This application may not be used everyday. So ideally I don't want that method to be bound to let say Form Load of the main Form. How can this be achieved?
You probably don't want that logic in your client application. There are three ways that come to mind:
put the logic in the SQL Server and create a job that is scheduled to run every day
create a small utility application and schedule that to once each day
create a windows service that runs all the time and handles these jobs for you
If all the data necessary to make the determination of whether or not to send the notification e-mails is available in the database, and you have access to create a job on the SQL server, I would recommend that route.
However if there are external components that you need in order to make the determinations or to send the e-mails then either approach 2 or 3 will be the way to go. Creating an application and scheduling it to run each day would be easier to implement but a service has the benefit of not requiring an interactive logon session (i.e. doesn't need a user to be actively logged in on the computer) which is preferable on a server.

Tracking Microsoft SQL Server Activity

I have a very strange and complicated situation. I have data being erased from one of my SQL Server tables, and I am not sure by what application. I would like to be able to track this.
As I am sure you are wondering how I could find myself in this situation, here is some background. We have 2 servers, Web and Database running IIS6 and SQL Server 2005 respectively. They were setup by the previous developer who left the company without giving me any sort of introduction to the system so I am left "hunting" for everything.. I have been able to figure out most of the system on my own except for this, which remains a mystery. All I know for sure is this:
Data is being erased at a set time every day (I have setup a TRIGGER to capture this)
It is not a SQL Server Agent Job
It is not a Windows Scheduled Task
It is not a Windows Service
All database logins are done with the sa user so login history cannot help me... (again, I didn't set this up)
How the heck do I debug something like this? If anything, I want to know if this is coming from something running on the database server, or from a request from an outside source. Please help :-)
As you know the time it happens you should set up a SQL Profiler trace at that time to catch the statements being sent.
This will show you the SQL being sent, the spid of the connection, user name, application name sent by the connection and other useful info to track down the culprit.
In case the time that it happens is not convenient for you to do this you can script SQL traces (which is more lightweight than running the full GUI anyway)
Edit: Be careful when using it not to record so much information that you bog down the server. You can filter for activity on the database of interest for example.

Sending a summary of SQL Server Agent job failures

SQL Server Agent allows you to create Notifications at the moment a Job succeeds or fails, but I'd like to create a regular notification that sends a summary of the events for those who are one step removed from server maintenance.
Is there a way to setup summary notifications that spell out which jobs failed over the last 24 hours?
There are several system stored procedures that you might be able to use or you could query directly against the system tables. If you have SSRS available then put together a report using those queries and you can have it on-demand or scheduled to be emailed to the necessary people at whatever time is right for you.
Specifically, check out sp_help_jobhistory and sp_help_jobhistory_full in msdb.
I'd be surprised if you couldn't use Google to find a demo of setting this up.

Resources