Login information

Posted on 2016-08-16
Last Modified: 2016-08-19
Query to find user name in sql query.
Need to update a field in table which will store the username who isconnected to sql server.

Question by:RIAS
  • 3
  • 2
  • 2
  • +2
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41758215

Open in new window

will return the name of the logged on user in SSMS.  But I don't understand the purpose of this?  Are you doing this through code, and if so can't you pass the environment variable for username through that way?
LVL 20

Expert Comment

by:Russ Suter
ID: 41758217
SELECT SYSTEM_USER will tell you the login in use.
SELECT CURRENT_USER will tell you the user.

These aren't necessarily the same thing. It depends on the user-login mapping for the particular database.
LVL 25

Expert Comment

ID: 41758219
SQL Server is a multi user RDBMS so in many situations, many users could be connected. However if it is known to be a single user situation where it is just an app with SQL Server these might help:

2008 and forward:
SELECT CURRENT_USER;      this will return the current user

SELECT SYSTEM_USER;         gets users currently executing the script

You can use  normal TSQL
UPDATE sometablename SET somefield=whatever
WHERE somecondition is true

with that information to do what you want I guess

Without a more clear explanation, help can't be further provided.

Author Comment

ID: 41758245
Just want an audit trail
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

LVL 12

Expert Comment

by:Dustin Saunders
ID: 41758275
What are you auditing?  Users in SSMS?  Are you intending for this to be a trigger or something?
LVL 25

Accepted Solution

SStory earned 500 total points
ID: 41758313

Expert Comment

by:Senior IT System Engineer
ID: 41758701
What about

exec sp_who2

Open in new window

would that be helpful ?

Author Comment

ID: 41758975
      [SQL] [nvarchar](2000) NULL,
      [EntryDateTime] [datetime] NULL CONSTRAINT [EntryDateTime]  DEFAULT (getdate()),
      [User] [nvarchar](2000) NULL   CONSTRAINT   DEFAULT ((SYSTEM_USER)),

Any suggestions on this ?
LVL 25

Expert Comment

ID: 41762766
Are you getting an error? You do have an extra comma that I think shouldn't be there after SYSTEM_USER))

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Select2 jquery help 9 45
sql server query? 6 26
Row-Level Security 2 19
Sql query to Stored Procedure 6 15
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

705 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

19 Experts available now in Live!

Get 1:1 Help Now