• Imprimir

--> Criando tabela auxiliar para rodar o script

CREATE TABLE ANALISE_TABELA1_TABELA2([TABELA1] [varchar](50) , [CAMPO1] [varchar](100) )

--> select * from ANALISE_TABELA1_TABELA2 (Caso prescise visualizar)

--> drop table ANALISE_TABELA1_TABELA2 (Deletar a tabela depois do exercicio)

 

--> Criando uma tabela local (espelho) da tabela do servidor remoto (supondo que a tabela2 esteja em outro servidor)

select * into <<SERVIDOR REMOTO>>_<<TABELA REMOTA>> from <<SERVIDOR REMOTO>>.<<BANCO REMOTO>>.dbo.<<TABELA REMOTA>>

--> drop table <<SERVIDOR REMOTO>>_<<TABELA COPIADA DO SERVIDOR REMOTO>>(Deletar a tabela depois do exercicio)

 

 

--> TRUNCATE TABLE ANALISE_TABELA1_TABELA2 (Caso prescise apagar dados da tabela)

--> Armazenando dados tabela local

insert  into  ANALISE_TABELA1_TABELA2 ([TABELA1],[CAMPO1])  select '<<TABELA LOCAL>>',COLUMN_NAME from information_schema.columns where table_name = '<<TABELA LOCAL>>'

--> Armazenando dados tabela  (espelho)

insert into  ANALISE_TABELA1_TABELA2 ([TABELA1],[CAMPO1]) select '<<TABELA COPIADA DO SERVIDOR REMOTO>>',COLUMN_NAME from information_schema.columns where table_name = 'TABELA COPIADA DO SERVIDOR REMOTO'

 

--> Listando campos que existem nas duas tabelas

SELECT campo1+',', Count(*) FROM ANALISE_TABELA1_TABELA2 GROUP BY  campo1 HAVING Count(*) > 1

 

--> Listando campos que só existem em uma tabela

SELECT campo1, Count(*) FROM ANALISE_TABELA1_TABELA2 GROUP BY  campo1 HAVING Count(*) = 1

 

--> Desafio aos alunos ( Listar campos que só existem na tabela1 )