一次性更改数据库內拥有者不屬dbo的物件改成为dbo

Create PROCEDURE SP_ChangeObjectOwnerToDBO 
AS
SET NOCOUNT ON

DECLARE @objName varchar(80),    @ownerName varchar(80), @Parameter    varchar(80)

DECLARE object_cursor CURSOR FOR

-- Find out table list not belong owner
select o.name, user_name(o.uid)
from dbo.sysobjects o, dbo.sysindexes i where OBJECTPROPERTY(o.id, N'IsTable') = 1 and i.id = o.id

and i.indid < 2 and o.name not like N'#%' and xtype='U'
and user_name(o.uid)<>'dbo' order by o.name

OPEN object_cursor
FETCH NEXT FROM object_cursor INTO @objName, @ownerName

PRINT''
PRINT 'Process Table(s) '
PRINT '--------------------------------------------'

IF @@FETCH_STATUS <> 0 
PRINT 'No Objects Affected.'

WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @Parameter = @ownerName+'.'
PRINT @Parameter

-- Change Object Owner ---
exec sp_changeobjectowner @Parameter , 'dbo'

FETCH NEXT FROM object_cursor INTO @objName, @ownerName
PRINT''
END

CLOSE object_cursor
DEALLOCATE object_cursor
-------------------

DECLARE object_cursor CURSOR FOR

-- Find out View list not belong owner
select o.name, user_name(o.uid)
from dbo.sysobjects o where OBJECTPROPERTY(o.id, N'IsView') = 1 and o.name not like N'#%%' 
and user_name(o.uid)<>'dbo' order by o.name

OPEN object_cursor
FETCH NEXT FROM object_cursor INTO @objName, @ownerName

PRINT''
PRINT 'Process View(s) '
PRINT '--------------------------------------------'

IF @@FETCH_STATUS <> 0 
PRINT 'No Objects Affected.'

WHILE @@FETCH_STATUS = 0
BEGIN

select @Parameter = @ownerName+'.'
print @Parameter

-- Change Object Owner ---
exec sp_changeobjectowner @Parameter , 'dbo'

FETCH NEXT FROM object_cursor INTO @objName, @ownerName
print ''
END

CLOSE object_cursor
DEALLOCATE object_cursor
--------------------------------

DECLARE object_cursor CURSOR FOR

-- Find out Store Proc    list not belong owner
select o.name, user_name(o.uid)
from dbo.sysobjects o where (OBJECTPROPERTY(o.id, N'IsProcedure') = 1 or OBJECTPROPERTY(o.id,

N'IsExtendedProc') = 1 or OBJECTPROPERTY(o.id, N'IsReplProc') = 1) and o.name not like N'#%%'   
and user_name(o.uid)<>'dbo' order by o.name

OPEN object_cursor
FETCH NEXT FROM object_cursor INTO @objName, @ownerName

PRINT''
PRINT 'Process Store Procedure(s) '
PRINT '--------------------------------------------'

IF @@FETCH_STATUS <> 0 
PRINT 'No Objects Affected.'

WHILE @@FETCH_STATUS = 0
BEGIN

select @Parameter = @ownerName+'.'
print @Parameter

-- Change Object Owner ---
exec sp_changeobjectowner @Parameter , 'dbo'

FETCH NEXT FROM object_cursor INTO @objName, @ownerName
PRINT''
END

CLOSE object_cursor
DEALLOCATE object_cursor

-----------------------------------------

DECLARE object_cursor CURSOR FOR

-- Find out User Define Func list not belong owner
select o.name, user_name(o.uid)
from dbo.sysobjects o          where (o.xtype = N'TF' or o.xtype = N'FN' or o.xtype = N'IF') and

o.name not like N'#%%'  
and user_name(o.uid)<>'dbo' order by o.name

OPEN object_cursor
FETCH NEXT FROM object_cursor INTO @objName, @ownerName

PRINT''
PRINT 'Process User Define Function(s) '
PRINT '--------------------------------------------'

IF @@FETCH_STATUS <> 0 
PRINT 'No Objects Affected.'

WHILE @@FETCH_STATUS = 0
BEGIN

select @Parameter = @ownerName+'.'
print @Parameter

-- Change Object Owner ---
exec sp_changeobjectowner @Parameter , 'dbo'

FETCH NEXT FROM object_cursor INTO @objName, @ownerName
PRINT''
END

CLOSE object_cursor
DEALLOCATE object_cursor

Set Nocount off
GO

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值