俺只測試了表和預存程序,其它對象未有實際測試,
使用前請備份資料庫,
任何錯誤俺概不負責 ^_^。
1 /*
2 brief: 改變資料庫所有對象的所有者
3 author: 吳爾平
4 date: 2005/12/19
5 */
6
7 declare @owner varchar(255)
8 set @owner = 'dbo'
9
10 declare @objectname varchar(255)
11 declare @uid int
12 declare object_cursor cursor for select [name],uid from sysobjects
13 where OBJECTPROPERTY (id, 'IsMSShipped') = 0
14 and (xtype like '%') -- 如果要指定對象的類型,參考 Transact-SQL 協助-> sysobjects-> xtype
15 open object_cursor
16 fetch next from object_cursor into @objectname, @uid
17 while(@@fetch_status=0)
18 begin
19 declare @uidName varchar(255)
20 select @uidName = [name] from dbo.sysusers where uid = @uid
21 if @uidName <> @owner
22 begin
23 --產生 '所有者.對象名' 格式的字串
24 set @objectname = @uidName + '.' + @objectname
25 --改變對象所有者
26 exec sp_changeobjectowner @objectname, @owner
27 end
28
29 fetch next from object_cursor into @objectname, @uid
30 end
31 close object_cursor
32 deallocate object_cursor
33 go