SQL Server Enterprise Manager - Eliminazione di massa delle tabelle e modifica proprietà delle tabelle

StackOverflow https://stackoverflow.com/questions/3736230

Domanda

Non ho praticamente alcuna esperienza con Enterprise Manager di SQL Server in modo non sono sicuro se questo è ancora possibile (o si spera ridicolmente semplice!)

Nel corso di un'importazione in un database è successo qualcosa in cui ogni tavolo si è duplicato con due differenze importanti.

La prima è che il proprietario su entrambi i tavoli è diversa, la seconda è che solo la struttura ha copiato tutto su una delle copie.

La legge di Sod ha indicato che, naturalmente, i dati sono stati memorizzati sui tavoli di proprietà della persona sbagliata, quindi la mia domanda è: posso rapidamente eliminare tutte le tabelle di proprietà di un utente e posso cambiare rapidamente la proprietà di tutte le altre tabelle di portare loro in linea.

Ci sono tavoli a sufficienza che l'automazione sta per essere la mia opzione preferita da un lungo cammino!

Qualsiasi aiuto sarebbe molto apprezzato, Sono in esecuzione SQL Server 2000

È stato utile?

Soluzione

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

Altri suggerimenti

Per cambiare proprietà, vedere: SQL Tabella del passaggio di proprietà, facile e veloce

Il codice dato nel link qui sopra è il seguente:

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top