[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

MS SQL 2014 get SPIDs of users

Posted on 2016-11-04
6
Medium Priority
?
71 Views
Last Modified: 2016-11-22
I have an application that uses MS SQL Server 2104 as its database engine. Due to government regulations I need to start tracking the amount of time users spend working each day. They come in each day, log into an application and then log out at the end of the day. In addition to the user ID and log in and out times I was thinking of adding the SQL SPID of each user so later I could investigate any locking issues. How do I go about getting the SPID once the user creates a new session?

I was thinking about having a table structured like this:
USERID char(3)
LoginTime datetime
LogoutTime datetime
RowID int (unique)
SPID int

When the user logins I insert a record with the User ID and login time. When the user logs out I update the logout time using the RowID. I am hoping the add the SPID as an additional point of information.
0
Comment
Question by:rwheeler23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 40

Accepted Solution

by:
lcohan earned 2000 total points
ID: 41874285
You could use SQL own functionality rather than write your own like described here:
https://mssqltalks.wordpress.com/2013/02/25/how-to-audit-login-to-my-sql-server-both-failed-and-successful/
"Using SQL Server management Studio: Connect instance in Management Studio → Right Click and select  server Properties → Security → Under Login Auditing select both failed and Successful logins and click OK.  This will audit all the login attempts in Error log."

OR "SQL Server – Login Auditing using Logon Triggers" like described here:
https://sqlandme.com/2011/07/13/sql-server-login-auditing-using-logon-triggers/
0
 

Author Comment

by:rwheeler23
ID: 41874308
That is one way to do that however our application logs into SQL as the same user so SQL would see the same user ID.  We pass down a User ID that our application uses so each inserted record will be assigned that value as the record is inserted. That is why I was looking to do this myself.
0
 
LVL 40

Expert Comment

by:lcohan
ID: 41874372
Well as far as I'm aware there are no "LOGOUT" SQL Servers triggers but for the LOGON audit you could use a trigger like below:

CREATE TABLE LogonAudit
(
    AuditID INT NOT NULL CONSTRAINT PK_LogonAudit_AuditID
                PRIMARY KEY CLUSTERED IDENTITY(1,1)
    , UserName NVARCHAR(255)
    , LogonDate DATETIME
    , spid INT NOT NULL
    , ClientIP SYSNAME
);
GO
GRANT INSERT ON dbo.LogonAudit TO public;
GO
CREATE TRIGGER LogonAuditTrigger ON ALL SERVER FOR LOGON
AS
BEGIN
    --IF SUSER_SNAME() <> 'sa'
    INSERT INTO dbo.LogonAudit (UserName, LogonDate, spid, ClientIP)
            VALUES (SUSER_SNAME(), GETDATE(), @@SPID, (SELECT client_net_address  as ipaddess FROM sys.dm_exec_connections WHERE session_id=@@SPID) );
END;
GO
ENABLE TRIGGER LogonAuditTrigger ON ALL SERVER;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:rwheeler23
ID: 41874516
Well since they are logging out via my application I could create a logout routine in there. If they are foolish enough to not log out at night I will know that and I can go pound on their heads. This should be enough to get me started.
0
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 2000 total points
ID: 41897999
Did the trigger posted in previous comment helped you at all with the Audit process?
0
 

Author Closing Comment

by:rwheeler23
ID: 41898345
The triggers did help but not for this application. Behind the scenes, the embedded connect string uses 'sa' for everyone. My application passes it own user ID so writing to a table made more sense.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

656 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