--下面的SQL语句可以直接更改一个表的所有者,前提是要testuser用户存在
--EXEC sp_changeobjectowner 'dob.product', 'testuser'
----下面是通过修改系统表的相应值来达到修改所有者的目的。在sql2000下可用。在2005下还没有测试。
----创建存储过程
--更改单个表的所有者
if exists ( select name from sysobjects where name = 'ChangeTableOwner' and type= 'P')
drop procedure ChangeTableOwner
go
create procedure ChangeTableOwner
@TableName varchar(50),
@newUserName varchar(50)
as
Begin
exec sp_configure 'allow updates', 1
RECONFIGURE WITH OVERRIDE
declare @newId int
if exists (select uid from sysusers where name = @newUserName)
BEGIN
select @newId = uid from sysusers where name = @newUserName
update sysobjects set uid = @newId where name=@TableName and type='u'
END
else
BEGIN
print N'the @newUserName does not exist,please check it and try again!'
END
exec sp_configure 'allow updates', 0
RECONFIGURE WITH OVERRIDE
End
go
--更改所有表的所有者(不会更改系统表)
if exists ( select name from sysobjects where name = 'ChangeUser' and type= 'P')
drop procedure ChangeUser
go
create procedure ChangeUser
@newUserName varchar(50)
as
Begin
sp_configure 'allow updates', 1
RECONFIGURE WITH OVERRIDE
declare @newId int
if exists (select uid from sysusers where name = @newUserName)
BEGIN
select @newId = uid from sysusers where name = @newUserName
update sysobjects set uid = @newId where type='u'
END
else
BEGIN
print N'the @newUserName does not exist,please check it and try again!'
END
sp_configure 'allow updates', 0
RECONFIGURE WITH OVERRIDE
End
|