修改mssql的表和存储过程所有者为dbo的方法

    文章来源:万象互联 更新时间:2014-8-27 15:54:01
分享:

修改Mssql表所有者

1、批量修改MSSQL表的所有者
exec sp_MSforeachtable 'exec sp_changeobjectowner "?","dbo"'
2、单个修改MSSQL表的所有者:
exec sp_changeobjectowner '要改的表名','dbo

修改MSSQL存储过程所有者

现在列出这两段命令,可以修改MSQL数据库存储过程的所有者! 命令为两段,分两次执行!
--一次性执行如下语句:

CREATE PROCEDURE dbo.ChangeObjectOwner

@OldOwner as NVARCHAR(128)='dbo',--参数原所有者

@NewOwner as NVARCHAR(128)='dbo'--参数新所有者

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


完了再执行下一句:
exec ChangeObjectOwner '数据库名','dbo'

这样就可以完全将存储过程的所有者修改过来!

版权说明:本站原创文章,由万象互联SEO优化发表.
本文地址:https://www.hulian.top/zixun/post/6512.html
在线咨询
  • 在线时间
  • 8:00-21:00