Today, we worked on a service request that our customer reported this error Msg 15063, Level 16, State 1, Line 1 The login already has an account under a different user name. Following I would like to share our findings.
Our customer has an application that connecting with an user (dbManagerUser) that is dbmanager is creating a database in Azure SQL Database.
create login dbManagerUser WITH Password = 'XXX'
create user dbManagerUser from login [dbManagerUser]
ALTER ROLE dbmanager ADD MEMBER [dbManagerUser];
Once the database has been created our customer executes the query to associate the login with the user. Our customer has a small piece of code to verify if the user exists or not. After it, our customer got the error message: Msg 15063, Level 16, State 1, Line 1 The login already has an account under a different user name.
IF NOT EXISTS( SELECT top 1 * from sys.sysusers where Name='dbManagerUser')
BEGIN
create user dbManagerUser from login [dbManagerUser]
END
Please, pay attention that running sys.sysusers you are not going to see this user you are going to see the dbo instead of dbManagerUser due to this user is the owner of the database. For this reason, the check if exist or not in sys.sysusers always reported that doesn't exist. This situation happened, because creating the database under a specific user the owner (dbo) will be this user in the database. So, there is not possible to add.
CREATE USER [dbo] FOR LOGIN [dbManagerUser] WITH DEFAULT_SCHEMA=[dbo]
Even, if we try to create another user, for example, dbManagerUser1 using the login dbManagerUser we could receive the following error code: Msg 15063, Level 16, State 1, Line 3 The login already has an account under a different user name. Because there is not possible to have two USER for the same login.
Enjoy!