Send an email to current user in SQL Server Agent job when job fails - sql-server

I have a SQL Job where I want to send an email alert if job fails. I know that for this functionalty I have to Configure Database Mail and than Adjust the properties within the SQL Server Agent. This article gives pretty good detail http://www.sherweb.com/blog/how-to-set-up-database-mail-for-sql-server-job-failures/.
What I want is, instead of setting up an email address to particular user like person1#test.com, I want to send an email to current user. This way who ever logged in to database and runs the job receives the email on job failure.
As I see, there are two benefits of this approach,
i. One particular user won't get flooded with emails. ii. Who ever is running the job can know that job has failed.
For example,
If I set person1#test.com and person2 logs in than on job failure person1 will receive an email. I don't want that. Is there any way that I can do this in SQL Server Agent Job settings. If no than how can I achieve this?
Like SELECT SYSTEM_USER or SELECT SUSER_NAME() gives me current user and use that as outgoing email address.

I think you are confused, if one user will start the job manually then you use a STORE PROCEDURE and there you can get the connected user information.
But pgAgent will run the process automatic and run using the USER you setup on the job.

Related

How to set dynamic connection manager using SQL Server authentication which use password at run time from a table

I want to connect to SQL Server using SQL Server authentication in connection manager in SSIS. I want that every time when the package runs and connects to server it should use user id and password from the table created in SQL Server at run time.
The reason to get password from table is that every 6 months I need to change the password as per the policy so I don't want to login to each and every SSIS package and change the password there. I want to change the password in table and redirect that password through variable/expression to the connection manager password window.
I will ignore the idea that saving passwords in plain text makes changing them every six months a futile effort. It is a far greater security risk to have them stored in plain text ANYWHERE.
The solution is to NOT use an SQL Login. Have these services run under a specific Windows User and grant that User the necessary access.
An alternative is to explain the problem to the Security admin and get an account setup that does not expire but that can only login locally as a service. Don't use that account for anything else.
HTH,
Sean

Restrict when users can log into SQL Server Management Studio by day/time

We have several users in our database that we only want to allow them access via their user between certain static times of the day, and only on certain days, I can't see anything in the user properties to do this so guessing it cant be done, just wanted to check if anyone on here knows of any way of doing it before I put this idea to bed?
Create a couple of SQL Server agent jobs that grant/revoke privileges, then schedule these to run at the appropriate times.
You can do this with a Logon Trigger that checks the login name and datetime and calls ROLLBACK if needed.
You would likely still need a scheduled job to handle (e.g. kill) already open connections belonging to these users at the cut off points when their access expires though.

Unable to access Linked server tables via job in SQL Server

I have a job which tries to access data from a remote server. I have created linked server which is connected successfully. When I try to access tables by directly running the query it works fine. But when I run same query via job it throws an error "login failed for user".
Job is assigned owner 'sa' and running with ssis proxy with sysadmin and public roles. In security tab of linked server properties I have no mappings and "be made using the login's current security context" selected.
I am not sure how should I correct it. If I should add mappings then what should be mapped? Please help me as I already spend whole day exploring possible options but couldn't find anything useful.
It does not matter who is the owner of package. Only the user under whom the packet is launched is important.
If this user is Windows (Windows domen) user, read this link.
If your user is sql-user, check it password and permissions on linked server.
If all from previous step are correct, try to add this user to mapping with checked Impersonate checkbox.

Testing sp_send_dbmail

Does anyone know if there is a way to setup sql server to send all mail to one specific email address rather than what has been passed into sp_send_dbmail?
I ask as I need to verify what is going to be sent on a system that has customer email addresses and I don't want to actually send the email but rather check the contents.
The customer occasionally go onto this system to test certain things out which may trigger emails. At the moment these email are not sent out to anyone as the system is not setup for mail. I want this to stay the same but instead send all these mails to an internal email address. I don't know all the code that sends emails as there are approx. 3000 stored procedures in this system.
I have been searching on-line but can only find how to setup email.
If you're just looking to verify what will be sent, you can query the msdb.dbo.sysmail_mailitems table:
SELECT *
FROM msdb.dbo.sysmail_mailitems
This table holds the recipients, subject, body, format, query, sent_date, from_address, reply_to, and some other fields. I also suspect, though I haven't test, that if you updated that table to a new recipient email address that you could send them to the one address as you've requested above.
1)You can check mail is started or not using GUI.
2)you check email sending using following command.
Select * from msdb.dbo.sysmail_sentitems order by last_mod_date desc
enter image description here
unable to relay in Exchange Server :- Launch Exchange Server Manager and move to Administrative Groups. Select Administrative Group Name then choose Server>Server Name. Now click on Protocols and Select SMTP.
Right-click on Default SMTP Virtual Server and select Properties
In Access tab, click on Relay>only the list below
Now check the checkbox “Allow all systems to successfully authenticate to relay regardless of the list above” and close. ....check with internal team for permission.

Send automated email

I want to send automated emails to UserType( requester, assignee ,reviewer,admin) based on an insert and.or update in Database.
EX:
A remainder email send to requester, assignee ,reviewer and admin if the reviewer is not able to review the data in 7 days from the date it is assigned to him/her.
I know I can do this with SQL JOB or with Windows Services. But I ma looking for different way to do this if possible and/or does SSIS provide such functionality?
I think another option is to setup the trigger on insert/update of tables that need to be monitored then then called a stored procedure that use msdb.dbo.sp_send_dbmail to send out email.
You can look at the answer in this post for reference on how to send email using sp_send_dbmail.
Sending Email in SQL Server 2008 R2. I hope that helps.

Resources