Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - current user name

Which one should I use to record update made by users?

  1. SYSTEM_USER, or
  2. ORIGINAL_LOGIN(), or
  3. SUSER_SNAME()
like image 326
Sreedhar Avatar asked Nov 04 '10 22:11

Sreedhar


People also ask

How can I see current username in SQL?

SQL Server USER_NAME() Function The USER_NAME() function returns the database user name based on the specified id. If no id is specified, this function will return the name of the current user.

How do I find my SQL username and password?

Pass the userid and password to the procedure. Storeprocedure(uname varchar,pwd varchar) { check whether the record exists for that uname and pwd. (something like select count(*) into intcount from users where upper(username)=uname and password=pwd) if intcount>0 return true. else return false. }

How do I find the current server name in SQL Server?

Open up SQL Server Configuration Manager (search for it in the Start menu). Click on SQL Server Services . The instance name of SQL Server is in parenthesis inline with SQL Server service. If it says MSSQLSERVER, then it's the default instance.

What is SQL user ID?

Calls to USER_ID by a Windows principal mapped to an implicit user will return the ID of the implicit user. USER_ID can be used in a select list, in a WHERE clause, and anywhere an expression is allowed. For more information, see Expressions (Transact-SQL).


1 Answers

SYSTEM_USER returns the current executing context, so this can return an impersonated context

ORIGINAL_LOGIN() returns the identity of the user that initially connected to the instance, so regardless whether the context is impersonated or not it will yield the original user that logged in, good for auditing.

SUSER_SNAME() this is used if you want to get the username by SID so SUSER_SNAME can be invoked with a parameter like such SUSER_SNAME([server_user_sid]) but the SID is optional if you don’t pass that parameter the current user is returned.

like image 158
Raymund Avatar answered Nov 04 '22 04:11

Raymund