Solved

Alert mechanism if there is a particular login attempted in SQL server

Posted on 2016-10-25
3
25 Views
Last Modified: 2016-11-04
Hi there,
  Is there any way i could get alert from SQL server if the particular login is attempted in SQL server?

Eg., particular User named "Robin" attempts to login SQL server , then can i setup an Alert mechanism which should send an email to me stating "Robin" has logged in to our database server.

If yes, please share me the scripts/steps to setup the same.

Note: My environment running with SQL server 2008 R2 service pack 2 enterprise edition.

thanks
Deepak
0
Comment
Question by:Deepak Kumar
3 Comments
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
You can create a script and their you can check the name, Name of the user you can get like...


SELECT SUSER_NAME()  

GO  

O/P

XXX\pawankhowal
0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
Comment Utility
The mechanism that you want is Logon Trigger. With that you can test the login name and send an email if it matches what you want. Something like:
CREATE TRIGGER login_check ON ALL SERVER 
FOR LOGON  
AS  
BEGIN  
IF ORIGINAL_LOGIN()= 'Robin' 
    EXEC msdb.dbo.sp_send_dbmail  
        @profile_name = 'SQL Server',  
        @recipients = 'firstname.lastname@companyname.com',  
        @body = 'Robin just logged in SQL Server',  
        @subject = 'Logon' ; 
END;

Open in new window

0
 

Author Closing Comment

by:Deepak Kumar
Comment Utility
Thanks Vitor for your help
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
t-sql money data type decimal places 4 25
SQL Help - 12 34
IN with @variable 5 15
MSSQL Frequency of Years From Days Field 2 11
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now