sexta-feira, 22 de abril de 2016

SQL Server - Comparar dois bancos e listar diferenças

Para quem tem dificuldades em manter atualizadas as estruturas de seus clientes, vou postar uma query que compara dois Bancos de Dados. Vejam a query e observem os comentários:

-- BANCO A
use Quantum
SELECT CONCAT(t.name, '.', c.name, ' (', tp.name, ', ', c.length, ', ', c.isnullable, ')') TabelaCampoTipoTamanhoNull, t.name Tabela, c.name Campo, tp.name Tipo, c.length Tamanho, c.isnullable PerniteNull 
into #EstruturaA
FROM SYSCOLUMNS c
inner join SYSOBJECTS t on t.id = c.id
inner join SYSTYPES tp on tp.xtype = c.xtype
where t.xtype = 'U'
go

-- BANCO B
use QuantumNovo
SELECT CONCAT(t.name, '.', c.name, ' (', tp.name, ', ', c.length, ', ', c.isnullable, ')') TabelaCampoTipoTamanhoNull, t.name Tabela, c.name Campo, tp.name Tipo, c.length Tamanho, c.isnullable PerniteNull
into #EstruturaB
FROM SYSCOLUMNS c
inner join SYSOBJECTS t on t.id = c.id
inner join SYSTYPES tp on tp.xtype = c.xtype
where t.xtype = 'U'
go

-- Lista as Tabelas e Campos que só existem no A, e logicamente não existem no B 
SELECT TabelaCampoTipoTamanhoNull ExisteSomenteNoBancoB, Tabela, Campo, Tipo, Tamanho, PerniteNull
FROM #EstruturaB
where TabelaCampoTipoTamanhoNull
not in (SELECT TabelaCampoTipoTamanhoNull
FROM #EstruturaA)

-- Lista as Tabelas e Campos que só existem no B, e logicamente não existem no A 
SELECT TabelaCampoTipoTamanhoNull ExisteSomenteNoBancoA, Tabela, Campo, Tipo, Tamanho, PerniteNull
FROM #EstruturaA
where TabelaCampoTipoTamanhoNull
not in (SELECT TabelaCampoTipoTamanhoNull
FROM #EstruturaB)

Nenhum comentário:

Postar um comentário

Oracle - Listar datas do mês

 select TRUNC(SYSDATE)  + level - 1 dt from   dual connect by level <= (   LAST_DAY(SYSDATE) - TRUNC(SYSDATE) + 1 )