还是老习惯,下面给出更改数据库表,存储过程及视图所有者的SQL脚本,需要说明的是,这段脚本同样可以用于将数据库对象的所有者由DBO用户更改为其它指定的用户,使用的方法为:将YourUserName更改为DBO,在原有DBO的位置输入想要指定的用户账号名称,执行即可,当然,这段脚本代码在使用的时候是狠灵活的,并非只针对关于DBO与其它指定账号之间的更改,实践一下就自然明白了
1. 修改表的所有者
declare @tn varchar(120)
declare table_cursor cursor for
Select '[' + sysusers.name + '].' + sysobjects.name AS table_name
FROM sysobjects INNER JOIN sysusers ON sysobjects.uid = sysusers.uid
Where sysusers.name = 'YourUserName' AND sysobjects.type = 'U'
open table_cursor
fetch next from table_cursor into @tn
while @@FETCH_STATUS = 0
begin
exec sp_changeobjectowner @tn, 'dbo'
fetch next from table_cursor into @tn
end
close table_cursor
deallocate table_cursor
2. 修改存储过程的所有者
declare @tn varchar(120)
declare procedure_cursor cursor for
Select '[' + sysusers.name + '].' + sysobjects.name AS procedure_name
FROM sysobjects INNER JOIN sysusers ON sysobjects.uid = sysusers.uid
Where sysusers.name = 'YourUserName' AND sysobjects.type = 'P'
open procedure_cursor
fetch next from procedure_cursor into @tn
while @@FETCH_STATUS = 0
begin
exec sp_changeobjectowner @tn, 'dbo'
fetch next from procedure_cursor into @tn
end
close procedure_cursor
deallocate procedure_cursor
3. 修改视图的所有者
declare @tn varchar(120)
declare view_cursor cursor for
Select '[' + sysusers.name + '].' + sysobjects.name AS view_name
FROM sysobjects INNER JOIN sysusers ON sysobjects.uid = sysusers.uid
Where sysusers.name = 'YourUserName' AND sysobjects.type = 'V'
open view_cursor
fetch next from view_cursor into @tn
while @@FETCH_STATUS = 0
begin
exec sp_changeobjectowner @tn, 'dbo'
fetch next from view_cursor into @tn
end
close view_cursor
deallocate view_cursor