------------------------------------
修改SQLSERVER的表的所有者
by xie 2007-12-24 QQ:229315679
------------------------------------
declare @olduser varchar(50)
set @olduser = 'hzbts'
-------------------------
Declare @v1 varchar(255)
DECLARE C1 CURSOR
FOR select name from sysobjects where type='u'
OPEN C1
FETCH NEXT FROM C1 INTO @v1
WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2)
BEGIN
set @v1 = @olduser + '.'+ @v1;
exec sp_changeobjectowner @v1 , 'dbo'
END
FETCH NEXT FROM C1 INTO @v1
END
CLOSE C1
DEALLOCATE C1
GO
--修改所有用户表/存储过程/视图/触发器/自定义函数的所有者为dbo,则用游标(不要理会错误提示)
declare tb cursor local for
select 'sp_changeobjectowner ''['+replace(user_name(uid),']',']]')+'].['
+replace(name,']',']]')+']'',''dbo'''
from sysobjects
where xtype in('U','V','P','TR','FN','IF','TF') and status>=0
open tb
declare @s nvarchar(4000)
fetch tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch tb into @s
end
close tb
deallocate tb
go