码迷,mamicode.com
首页 > 数据库 > 详细

关于SQL数据表存储过程表名前缀换成dbo代码

时间:2015-05-12 18:51:17      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:存储   数据   sql   数据库   dbo   

1.全部表名换成dbo

<span style="font-size:18px;">EXEC sp_MSforeachtable 'exec sp_changeobjectowner ''?'',''dbo'' '</span>

2.存储过程全部换成dbo

<span style="font-size:18px;">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 
</span>

然后在运行

<span style="font-size:18px;">exec ChangeObjectOwner '数据库','dbo' 
</span>


关于SQL数据表存储过程表名前缀换成dbo代码

标签:存储   数据   sql   数据库   dbo   

原文地址:http://blog.csdn.net/ddgweb/article/details/45672983

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!