问题场景

在SQL Server数据库迁移时,在另外一台服务器上恢复数据库备份文件之后,需要重新创建之前数据库上的用户帐户。在创建登录用户时,需要在User Mapping中给该用户针对具体的数据库进行授权,由于恢复出来的数据库中存在同名的用户帐户,创建时会出现"User, group, or role already exists in the current database"的错误提示。详细错误信息如下:

TITLE: Microsoft SQL Server Management Studio
------------------------------
Create failed for User 'testuser'.  (Microsoft.SqlServer.Smo)
------------------------------
ADDITIONAL INFORMATION:
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
------------------------------
User, group, or role 'testuser' already exists in the current database. (Microsoft SQL Server, Error: 15023)

之前的解决方法

在创建用户帐户(或者授权)之前,先在对应的数据库中删除该同名的用户帐户。

最新的解决方法

先创建用户帐户,不进行授权,然后通过下面的SQL语句将该用户帐户关联至对应的数据库用户。优点是避免了重新授权的操作。

USE {目标数据库}
EXEC sp_change_users_login 'Update_One', '{目标数据库已存在的用户名}', '{创建的登录用户名}'

方法来源

SQL Server Forums - How to assign a login to an existing user?

相关文章:

  • 2021-11-28
  • 2021-07-05
  • 2022-12-23
  • 2021-11-16
  • 2021-10-14
  • 2022-12-23
  • 2021-10-03
  • 2021-09-10
猜你喜欢
  • 2022-12-23
  • 2021-05-27
  • 2021-11-27
  • 2022-01-01
相关资源
相似解决方案