问题描述:
我恢复别人备份在别的机器上的SQL数据库到我的机器上的SQL SERVER,恢复后该数据库中有一个mis用户,该用户是所有者,很多表的所有者也是他,但我的安全性里的登录中没有该用户,我删除不了该用户,提示“因为选定的用户拥有对象,所以无法除去该用户”!
问题解决方式:
1 如果是一张表有此问题,解决方式如下:
exec sp_changeobjectowner mis.tablename', 'dbo'
这样就把tablename指定的表的所有者赋值给了dbo用户
2 如果所有表都存在此问题,这是就要编写存储过程实现批量修改了
IF OBJECT_ID('ChangeAll') IS NOT NULL DROP PROC ChangeAll
GO
CREATE PROCEDURE ChangeAll (
@old sysname,
@new sysname
)
AS
DECLARE @objname sysname
SET NOCOUNT ON
IF USER_ID(@old) IS NULL
BEGIN
RAISERROR ('The @old passed does not exist in the database', 16, 1)
RETURN
END
IF USER_ID(@new) IS NULL
BEGIN
RAISERROR ('The @new passed does not exist in the database', 16, 1)
RETURN
END
DECLARE owner_cursor CURSOR FOR
SELECT name FROM sysobjects WHERE uid = USER_ID(@old)
OPEN owner_cursor
FETCH NEXT FROM owner_cursor INTO @objname
WHILE (@@fetch_status <> -1)
BEGIN
SET @objname = @old + '.' + @objname
EXEC sp_changeobjectowner @objname, @new
FETCH NEXT FROM owner_cursor INTO @objname
END
CLOSE owner_cursor
DEALLOCATE owner_cursor
GO
EXEC ChangeAll @old = 'mis', @new = 'dbo'
这样就完成了批量修改权限的工作
详细信息请参考:http://www.itpub.net/429496.html