原帖地址 下面的操作存在风险 建议做好数据库备份 1 单个修改 exec sp_changeobjectowner '[所有者].[对象名]', 'dbo' 例子: exec sp_changeobjectowner '[test].[dnt_users]', 'dbo' 执行后 数据库中dnt_users表的所有者从test变为dbo 2 批量修改 2.1 现在查询分析其中执行 下面的sql语句 建立名为changename存储过程
- CREATE PROCEDURE dbo.changename
- @OldOwner as NVARCHAR(128),--参数原所有者
- @NewOwner as NVARCHAR(128)--参数新所有者
- AS
- DECLARE @Name as NVARCHAR(128)
- DECLARE @Owner as NVARCHAR(128)
- DECLARE @OwnerName as NVARCHAR(128)
- DECLARE curObject CURSOR FOR
- select 'Name' = name,
- 'Owner' = user_name(uid)
- from sysobjects
- where user_name(uid)=@OldOwner
- order by name
- OPEN curObject
- FETCH NEXT FROM curObject INTO @Name, @Owner
- WHILE(@@FETCH_STATUS=0)
- BEGIN
- if @Owner=@OldOwner
- begin
- set @OwnerName = @OldOwner + '.' + rtrim(@Name)
- exec sp_changeobjectowner @OwnerName, @NewOwner
- end
- FETCH NEXT FROM curObject INTO @Name, @Owner
- END
- close curObject
- deallocate curObject
- GO
复制代码
2.2 执行存储过程changename exec changename '原所有者名','新所有者名' 例如 exec changename 'test','dbo' 则当前数据库中所有对象所有者 从 test 变为 dbo |