user without login sp_change_users_login

本文介绍如何解决SQL Server中孤儿用户的问题,通常发生在从一台服务器还原到另一台服务器时,原有的登录名不再存在而导致用户成为孤儿。文章提供了解决方案,包括报告孤儿用户、修复已知登录名的孤儿用户以及为孤儿用户创建新的登录名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-change-users-login-transact-sql?redirectedfrom=MSDN&view=sql-server-ver15
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/7d129880-048c-4834-a72e-c31ead4897cb/database-user-without-login?forum=sqlsecurity

DB Users which are not associated with SQL Logins will be called as Orphan ID's. You can either delete it from DB level or you can create a login in SQL and map the orphan ID with the newly created ID.
This situation will araise when you restore a database from different server (i.e from server1 to server2) where server1 will the logins associated with the db user and when you restore in server2 these logins might not exist in server2 and hence it will be an orphan ID in server2 db.


--First, make sure that this is the problem. This will lists the orphaned users:
EXEC sp_change_users_login 'Report'

--If you already have a login id and password for this user, fix it by doing:
EXEC sp_change_users_login 'Auto_Fix', 'user'

--If you want to create a new login id and password for this user, fix it by doing:
EXEC sp_change_users_login 'Auto_Fix', 'user', 'login', 'password'
 

Check out the link below to fix the same.

--How to fix orphaned SQL Server users
https://www.fileformat.info/tip/microsoft/sql_orphan_user.htm
http://blogs.msdn.com/raulga/archive/2006/07/03/655587.aspx

--Login failures connecting to new principal after SQL Server Database Mirroring failover
https://www.mssqltips.com/sqlservertip/1166/login-failures-connecting-to-new-principal-after-sql-server-database-mirroring-failover/


SELECT 
  'create login [' + p.name + '] ' + 
  case when p.type in('U','G') then 'from windows ' else '' end +
  'with ' +
  case when p.type = 'S' then 'password = ' + master.sys.fn_varbintohexstr(l.password_hash) + ' hashed, ' +
  'sid = ' + master.sys.fn_varbintohexstr(l.sid) + 
  ', check_expiration = ' + case when l.is_expiration_checked > 0 then 'ON, ' else 'OFF, ' end + 
  'check_policy = ' + case when l.is_policy_checked > 0 then 'ON, ' else 'OFF, ' end +
  case when l.credential_id > 0 then 'credential = ' + c.name + ', ' else '' end 
  else '' end +
  'default_database = ' + p.default_database_name +
  case when len(p.default_language_name) > 0 then ', default_language = ' + p.default_language_name else '' end
FROM sys.server_principals p 
LEFT JOIN sys.sql_logins l ON p.principal_id = l.principal_id 
LEFT JOIN sys.credentials c ON  l.credential_id = c.credential_id
WHERE p.type in('S','U','G')
AND p.name <> 'sa'

`sp_change_users_login` 是一个系统存储过程,用于将数据库用户与 SQL Server 登录名进行关联或修复它们之间的不匹配。以下是对该存储过程的详细解释和用法: ### 解释 `sp_change_users_login` 主要用于解决数据库用户和 SQL Server 登录名之间的映射问题。当数据库用户和登录名之间存在不匹配时,可以使用该存储过程来修复这些问题。 ### 具体用法 你提供的代码如下: ```sql Use 数据库名 go sp_change_users_login 'update_one', '用户名', '用户名' ``` #### 参数解释 1. `'update_one'`:表示要执行的操作是更新单个用户。 2. `'用户名'`:第一个 `'用户名'` 是数据库中的用户名称。 3. `'用户名'`:第二个 `'用户名'` 是 SQL Server 登录名。 ### 使用步骤 1. **选择数据库**:使用 `USE` 语句选择你要操作的数据库。 2. **执行存储过程**:调用 `sp_change_users_login` 并传入相应的参数。 ### 示例 假设你有一个数据库名为 `MyDatabase`,用户名为 `MyUser`,登录名为 `MyLogin`,可以使用以下代码来关联用户和登录名: ```sql USE MyDatabase GO sp_change_users_login 'update_one', 'MyUser', 'MyLogin' ``` ### 详细步骤 1. **打开 SQL Server Management Studio (SSMS)** 并连接到你的 SQL Server 实例。 2. **打开一个新的查询窗口**。 3. **输入上述代码**,并确保将 `'MyDatabase'`, `'MyUser'`, 和 `'MyLogin'` 替换为实际的数据库名、用户名和登录名。 4. **执行查询**:点击执行按钮或按 F5 键。 ### 注意事项 - 该存储过程在 SQL Server 2000 到 SQL Server 2012 中可用。在更高版本的 SQL Server 中,推荐使用 `ALTER USER` 语句来更改用户登录名。 - 确保你有足够的权限来执行该存储过程。 ### 替代方法(适用于 SQL Server 2012 及以上版本) 在 SQL Server 2012 及以上版本中,可以使用 `ALTER USER` 语句来关联用户和登录名。例如: ```sql USE MyDatabase GO ALTER USER MyUser WITH LOGIN = MyLogin ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kenliang18

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值