[英]SQL Server Enterprise Manager - Mass Delete of Tables and Changing Ownership of Tables
我對SQL Server的企業管理器幾乎沒有任何經驗,所以我不確定這是否可行(或者希望這很簡單!)
在導入數據庫期間,發生了一些事情,其中每個表都有兩個重要的差異。
第一個是兩個表上的所有者是不同的,第二個是只有結構復制了其中一個副本。
Sod的法律表明,當然數據存儲在錯誤的人所擁有的表中,所以我的問題是我可以快速刪除一個用戶擁有的所有表,並且我可以快速更改所有其他表的所有權以使它們排成一行。
有足夠的表格,自動化將成為我的首選方式!
任何幫助將不勝感激,我正在運行SQL Server 2000
declare @emptyOwner varchar(20)
declare @wrongOwner varchar(20)
declare @emptyOwnerID bigint
declare @wrongOwnerID bigint
declare @tableName nvarchar(255)
set @emptyOwner = 'dbo'
set @wrongOwner = 'guest'
select @emptyOwnerID = (select uid from sysusers where name = @emptyOwner)
select @wrongOwnerID = (select uid from sysusers where name = @wrongOwner)
select name as tableName
into #tempTable
from systables
where type='U'
and exists (select 1 from systables where type = 'U' and uid = @emptyOwnerID)
and exists (select 1 from systables where type = 'U' and uid = @wrongOwnerID)
declare @dynSQL nvarchar(MAX)
declare ownme cursor for
select tableName from #tempTable
open ownme
fetch next from ownme into @tableName
while @@FETCH_STATUS = 0
begin
@dynSQL = 'DROP TABLE [' + @emptyOwner + '].[' + @tableName + ']'
exec(@dynSQL)
@dynSQL = 'sp_changeobjectowner ''[' + @wrongOwner + '].[' + @tableName + ']'',''' + @emptyOwner + ''''
exec(@dynSQL)
fetch next from ownme into @tableName
end
close ownme
deallocate ownme
有關更改所有權的信息,請參閱: SQL表所有權更改,快速簡便
上面鏈接中給出的代碼是:
DECLARE @old sysname, @new sysname, @sql varchar(1000)
SELECT
@old = 'oldOwner_CHANGE_THIS'
, @new = 'dbo'
, @sql = '
IF EXISTS (SELECT NULL FROM INFORMATION_SCHEMA.TABLES
WHERE
QUOTENAME(TABLE_SCHEMA)+''.''+QUOTENAME(TABLE_NAME) = ''?''
AND TABLE_SCHEMA = ''' + @old + '''
)
EXECUTE sp_changeobjectowner ''?'', ''' + @new + ''''
EXECUTE sp_MSforeachtable @sql
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.