Display the username in a column

We have a SQL Server 2012 database that 4 developers use, lets call them, user1, user2, user3, user4.

I want to create a column in one of the tables lets call the column User_Input, this column needs to show the username of the developer who insert any data, is this possible? For example if user2 inserted a new record, the column User_Input should display user2.

Please let me know if SQL Server does not support that, and if there is any other solution cause I searched the @@ functions for SQL Server and non of them seems to get the username.


SYSTEM_USER function will return the login name of the user.

You can test it out with this statement: SELECT SYSTEM_USER

The MSDN documentation for SYSTEM_USER states:

You can use the SYSTEM_USER function with DEFAULT constraints in the CREATE TABLE and ALTER TABLE statements. You can also use it as any standard function.

Here is a quick example of how to create a table with DEFAULT constraint that inserts the SYSTEM_USER into the User_Input column.

CREATE TABLE MyTable 
(
   ID int, 
   Value varchar(30), 
   User_Input varchar(200) DEFAULT SYSTEM_USER
)   

I believe this is what you're looking for.

SELECT SYSTEM_USER

You can add this to inserts/updates as needed.

CURRENT_USER provides the schema, not the login associated with the transaction.

链接地址: http://www.djcxy.com/p/5170.html

上一篇: WPF DataGrid ItemsSource绑定问题

下一篇: 在列中显示用户名