http://www.cnblogs.com/wantingqiang/archive/2008/12/14/1354738.html
有些時候,一個資料庫的管理員,需要將幾個不同的資料庫內的資料合併到一起,如果幾個資料庫的結構基本相同,只需新建一個資料庫,將其它幾個資料庫內的物件複製並追加到新資料庫內並做細微調整即可達到目的,但是,不同的資料庫可能存在不同的使用者帳號,資料庫物件 —— 譬如:表,儲存過程及視圖等 —— 的所有者也可能不同,這樣,在匯入資料時,系統會將同名但不同所有者的資料庫物件識別為不同的物件,會在目標庫中新建物件而非追加,並且會因為目標庫中沒有相應的使用者帳號而報使用者不存在的錯,那麼,統一資料庫物件的所有者就很必要了。
還是老習慣,下面給出更改資料庫表,儲存過程及視圖所有者的 SQL 腳本,需要說明的是,這段腳本同樣可以用於將資料庫物件的所有者由 DBO 使用者更改為其它指定的使用者,使用的方法為:將 YourUserName 更改為 DBO,在原有 DBO 的位置輸入想要指定的使用者帳號名稱,執行即可,當然,這段腳本代碼在使用的時候是很靈活的,並非只針對關於 DBO 與其它指定帳號之間的更改,實踐一下就自然明白了
- 修改表的所有者
declare @tn varchar(120)
declare table_cursor cursor for
Select '[' + sysusers.name + '].' + sysobjects.name AS table_name
FROM sysobjects INNER JOIN sysusers ON sysobjects.uid = sysusers.uid
Where sysusers.name = 'YourUserName' AND sysobjects.type = 'U'
open table_cursor
fetch next from table_cursor into @tn
while @@FETCH_STATUS = 0
begin
exec sp_changeobjectowner @tn, 'dbo'
fetch next from table_cursor into @tn
end
close table_cursor
deallocate table_cursor
- 修改儲存過程的所有者
declare @tn varchar(120)
declare procedure_cursor cursor for
Select '[' + sysusers.name + '].' + sysobjects.name AS procedure_name
FROM sysobjects INNER JOIN sysusers ON sysobjects.uid = sysusers.uid
Where sysusers.name = 'YourUserName' AND sysobjects.type = 'P'
open procedure_cursor
fetch next from procedure_cursor into @tn
while @@FETCH_STATUS = 0
begin
exec sp_changeobjectowner @tn, 'dbo'
fetch next from procedure_cursor into @tn
end
close procedure_cursor
deallocate procedure_cursor
- 修改視圖的所有者
declare @tn varchar(120)
declare view_cursor cursor for
Select '[' + sysusers.name + '].' + sysobjects.name AS view_name
FROM sysobjects INNER JOIN sysusers ON sysobjects.uid = sysusers.uid
Where sysusers.name = 'YourUserName' AND sysobjects.type = 'V'
open view_cursor
fetch next from view_cursor into @tn
while @@FETCH_STATUS = 0
begin
exec sp_changeobjectowner @tn, 'dbo'
fetch next from view_cursor into @tn
end
close view_cursor
deallocate view_cursor