How to change USER name

Coreysan 1,816 Reputation points
2023-07-20T23:55:50.74+00:00

I think this question is closer to what's going on.

Last month I logged in to our sql server using SSMS and Windows authentication. Then I typed:

SELECT USER (the result would show 'dbo')

But this month:

SELECT USER (the result shows 'MyDomain\Myname')

What I'm after is getting things back to 'dbo'. Can that be done? If so, how?

SQL Server | Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. ZoeHui-MSFT 41,536 Reputation points
    2023-07-21T05:50:00.02+00:00

    Hi @Coreysan,

    • SYSTEM_USER: returns Server Login name that was used to login to the instance (either SQL Server login or AD/Domain/Windows user-name).
    • USER, CURRENT_USER, USER_NAME(), or SESSION_USER: these all return Database User principal, which is (by default) dbo if you are db-owner or logged in as a sysadmin or sa.

    Please have a double check of your code.

    User's image

    Regards,

    Zoe Hui


    If the answer is helpful, please click "Accept Answer" and upvote it.


  2. Olaf Helper 47,586 Reputation points
    2023-07-21T06:26:20.1666667+00:00

    The system function "USER" returns the "database username". One SQL Server login can have different database username on different database. Are you connected to the same database as before? I guess not.

    See

    https://learn.microsoft.com/en-us/sql/t-sql/functions/user-transact-sql?view=sql-server-ver16

    0 comments No comments

  3. Erland Sommarskog 128.7K Reputation points MVP Volunteer Moderator
    2023-07-21T07:53:53.5833333+00:00

    SELECT USER would return dbo under one of these two conditions:

    1. You are the owner of the database.
    2. Your login is member of the fixed server role sysadmin role.

    I hope this information can help you to deduce what might have changed. You may have to talk to your DBA to have things changed back.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.