欢迎光临
我们一直在努力

ChangeAllObjectOwner-数据库专栏,SQL Server

建站超值云服务器,限时71元/月

exec changeallobjowner @oldowner = john, @newowner = alex

/*
version: sql server 7.0/2000
created by: alexander chigrik
http://www.mssqlcity.com/ – all about ms sql
(sql server articles, faq, scripts, tips and test exams).

this stored procedure can be used to run through all of a specific
databases objects owned by the oldowner and change the old
owner with the new one.
you should pass the old owner name and the new owner name,
as in the example below:

exec changeallobjowner @oldowner = john, @newowner = alex
*/

if object_id(changeallobjowner) is not null  //line continous
                  drop proc changeallobjowner
go

create procedure changeallobjowner (
  @oldowner sysname,
  @newowner sysname
)
as
declare @objname sysname
set nocount on

–check that the @oldowner exists in the database
if user_id(@oldowner) is null
  begin
    raiserror (the @oldowner passed does not exist in the database,
16, 1)
    return
  end
–check that the @newowner exists in the database
if user_id(@newowner) is null
  begin
    raiserror (the @newowner passed does not exist in the database,
 16, 1)
    return
  end

declare owner_cursor cursor for
  select name from sysobjects where uid = user_id(@oldowner)

open owner_cursor
fetch next from owner_cursor into @objname
while (@@fetch_status <> -1)
begin
  set @objname = @oldowner + . + @objname
  exec sp_changeobjectowner @objname, @newowner
  fetch next from owner_cursor into @objname
end

close owner_cursor
deallocate owner_cursor
go

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ChangeAllObjectOwner-数据库专栏,SQL Server
分享到: 更多 (0)

相关推荐

  • 暂无文章