【Vegas原创】将SQLServer表、视图、存储过程的所有者批量改为dbo的处理方法

现象: sqlserver2000的数据库备份还原到sqlserver2005不同的数据库名的数据库的时候,会出现表、视图、存储过程的所有者是原来数据库的用户名,造成数据库的存储过程、函数均无法访问。 还有一种现象,是数据库还原到sql2005后,数据库状态会变为sqlserver受限制用户。  

思路: 使用sp_changeobjectowner系统存储过程,通过游标的方式批量强制将数据库所有元素的所有者改为dbo,即可。

clip_image002 -》改为 clip_image004  

 

解决方案: 执行如下语句,即可。

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

  image

posted @ 2012-05-21 20:06  李济宏(Amadeus)  阅读(272)  评论(0编辑  收藏  举报