退换数据库中表的所属客户的五个艺术

DECLARE curObject CURSOR FOR select Name = name, Owner = user_name(uid)
from sysobjects where user_name(uid)=@OldOwner order by name

–更改某个表
exec sp_changeobjectowner ‘tablename’,’dbo’
–存储更改全部表
CREATE PROCEDURE dbo.User_ChangeObjectOwnerBatch
@OldOwner as NVARCHAR(128),
@NewOwner as NVARCHAR(128)
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
— select @name,@NewOwner,@OldOwner
FETCH NEXT FROM curObject INTO @Name, @Owner
END
close curObject
deallocate curObject
GO

先看单删除的

批量修改:
EXEC sp_MSforeachtable ‘exec sp_changeobjectowner ”?”,”dbo” ‘
单个修改:
EXEC sp_changeobjectowner ‘要改的表名’,’dbo’
如果想修改存储过程Owner:
先把–and   xtype=’p’
反注释,再创建sp_changeobjectowner 存储过程。
然后执行:EXEC sp_changeobjectowner ‘CurrentOwner’,’dbo’ 

close curObject deallocate curObject GO

declare proccur cursor for select [name] from sysobjects where name
like ‘Users_%’ declare @procname varchar(100)open proccurfetch next
from proccur into @procnamewhile(@@FETCH_STATUS = 0)begin exec(‘drop
proc ‘ + @procname) print(@procname + ‘已被删除’) fetch next from
proccur into @procnameendclose proccurdeallocate proccur

 

核心提示:因为一些网站是使用虚拟主机.用sql-server导数据上去的话

CREATE PROCEDURE ChangeProcOwner @OldOwner as
NVARCHAR(128),–参数原所有者 @NewOwner as NVARCHAR(128)–参数新所有者
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 and xtype=’p’ 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 ChangeProcOwner ‘xx’,’dbo’ 或者exec
ChangeProcOwner ‘?’,’dbo’

OPEN curObject
FETCH NEXT FROM curObject INTO @Name, @Owner
WHILE(@@FETCH_STATUS=0)
BEGIN   
if @Owner=@CurrentOwner 
begin
set @OwnerName = @CurrentOwner + ‘.’ + rtrim(@Name)
exec sp_changeobjectowner @OwnerName, @NewOwner
end  

DECLARE @Name as NVARCHAR(128) DECLARE @Owner as NVARCHAR(128) DECLARE
@OwnerName as NVARCHAR(128)

EXEC sp_MSforeachtable ‘exec sp_changeobjectowner ”?”,”dbo” ‘
单个修改表所有者: exec sp_changeobjectowner ‘要改的表名’,’dbo’

DECLARE curObject CURSOR FOR 
select ‘Name’   = name,
‘Owner’   = user_name(uid)
from sysobjects
where user_name(uid)=@CurrentOwner  –and   xtype=’p’
order by name 

发表评论

电子邮件地址不会被公开。 必填项已用*标注