首页 > 开发 > 综合 > 正文

更改数据库中表的所属用户的两个方法

2024-07-21 02:10:40
字体:
来源:转载
供稿:网友

--更改某个表
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)[email protected]
 order by name

open  curobject
fetch next from curobject into @name, @owner
while(@@fetch_status=0)
begin    
 if @[email protected]
 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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表