java,php,asp.net,linux,javascript,mysql,mssql,oracle,编程

更改数据库中表的所有者 sql

转载请注明:http://www.pmjava.com/Article/ShowInfo.asp?ID=56721

 

 

--下面的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

                                                                  
 

posted on 2009-06-12 18:18 rrong_m 阅读(151) 评论(0)  编辑  收藏

<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

随笔档案

文章分类

文章档案

java编程

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜