MERGE é um comando DML (Data Manipulation Language), assim como INSERT, DELETE e UPDATE. Apesar de não ser tão conhecido e usado quanto os outros DML’s, o MERGE merece uma atenção especial por ser um comando muito útil em vários tipos de casos, como sincronização de dados entre diferentes tabelas, carga de dados em processos ETL, entre outros.

 

Vamos para a parte prática, pois acredito que esta seja a maneira mais fácil de entender esta funcionalidade e de aprender como funciona.

Primeiro vou conectar ao schema HR e criar uma tabela para fazer os testes.

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”RDo+c3FscGx1cyBoci9ocgoKU1FMPiBDUkVBVEUgVEFCTEUgbW90bzIgQVMKICAyICBTRUxFQ1QgKiBGUk9NIG1vdG8KICAzICBXSEVSRSAxPTA7CgpUYWJlbGEgY3JpYWRhLg==” hover_enabled=”0″]RDo+c3FscGx1cyBoci9ocgoKU1FMPiBDUkVBVEUgVEFCTEUgbW90bzIgQVMKICAyICBTRUxFQ1QgKiBGUk9NIG1vdG8KICAzICBXSEVSRSAxPTA7CgpUYWJlbGEgY3JpYWRhLg==[/et_pb_dmb_code_snippet]

Criei uma tabela chamada “moto2”, com a mesma estrutura da tabela “moto”, que é uma tabela muito simples, conforme podem conferir abaixo:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”U1FMPiBTRUxFQ1QgKiBGUk9NIG1vdG87CgogICBNT1RPX0lEIE1PVE9fTk9NRSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBET05PCi0tLS0tLS0tLS0gLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIC0tLS0tLS0tLS0KICAgICAgICAgMSBIb3JuZXQgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICAyIENCUiAxMDAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDMgUjEgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNCBDRzEyNSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA1IENHMTUwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDYgQ0IzMDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNyBGYXplciA2MDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAoKNyBsaW5oYXMgc2VsZWNpb25hZGFzLg==” hover_enabled=”0″]U1FMPiBTRUxFQ1QgKiBGUk9NIG1vdG87CgogICBNT1RPX0lEIE1PVE9fTk9NRSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBET05PCi0tLS0tLS0tLS0gLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIC0tLS0tLS0tLS0KICAgICAgICAgMSBIb3JuZXQgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICAyIENCUiAxMDAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDMgUjEgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNCBDRzEyNSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA1IENHMTUwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDYgQ0IzMDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNyBGYXplciA2MDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAoKNyBsaW5oYXMgc2VsZWNpb25hZGFzLg==[/et_pb_dmb_code_snippet]

Agora vamos aos comandos MERGE!

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”U1FMPiBNRVJHRSBJTlRPIG1vdG8yIGEKICAyICAgIFVTSU5HIG1vdG8gYgogIDMgICAgICBPTiAoYS5tb3RvX2lkID0gYi5tb3RvX2lkKQogIDQgICAgV0hFTiBNQVRDSEVEIFRIRU4KICA1ICAgICAgVVBEQVRFIFNFVCBhLmRvbm8gPSBiLmRvbm8KICA2ICAgIFdIRU4gTk9UIE1BVENIRUQgVEhFTgogIDcgICAgICBJTlNFUlQgKG1vdG9faWQsIG1vdG9fbm9tZSwgZG9ubykKICA4ICAgICAgVkFMVUVTIChiLm1vdG9faWQsIGIubW90b19ub21lLCBiLmRvbm8pOwoKNyBsaW5oYXMgaW50ZXJjYWxhZGFzLg==” hover_enabled=”0″]U1FMPiBNRVJHRSBJTlRPIG1vdG8yIGEKICAyICAgIFVTSU5HIG1vdG8gYgogIDMgICAgICBPTiAoYS5tb3RvX2lkID0gYi5tb3RvX2lkKQogIDQgICAgV0hFTiBNQVRDSEVEIFRIRU4KICA1ICAgICAgVVBEQVRFIFNFVCBhLmRvbm8gPSBiLmRvbm8KICA2ICAgIFdIRU4gTk9UIE1BVENIRUQgVEhFTgogIDcgICAgICBJTlNFUlQgKG1vdG9faWQsIG1vdG9fbm9tZSwgZG9ubykKICA4ICAgICAgVkFMVUVTIChiLm1vdG9faWQsIGIubW90b19ub21lLCBiLmRvbm8pOwoKNyBsaW5oYXMgaW50ZXJjYWxhZGFzLg==[/et_pb_dmb_code_snippet]

O que fez o comando acima? Vamos para a “tradução”!

O comando MERGE foi aplicado na tabela “moto2“, usando como base a tabela “moto“. A chave de comparação entre as duas tabelas foi o campo “moto_id“, que é a chave primária das duas tabelas. Isso foi definido nas cláusulas USING e ON, respectivamente.

A cláusula WHEN MATCHED THEN disse o seguinte: “sempre que encontrar um registro na tabela moto em que a chave primária já exista na tabela moto2, atualize o campo dono da tabela moto2 com o valor do campo dono da tabela moto.”

A cláusula WHEN NOT MATCHED THEN disse o seguinte: “se um registro da tabela moto não foi encontrado na tabela moto2, tomando como base o campo moto_id, então insira o registro correspondente na tabela moto2“.

O exemplo acima foi bem simples. Após o comando, a tabela moto2, que antes estava vazia, ficou idêntica a tabela moto:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”U1FMPiBTRUxFQ1QgKgogIDIgIEZST00gbW90bzI7CgogICBNT1RPX0lEIE1PVE9fTk9NRSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIERPTk8KLS0tLS0tLS0tLSAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAtLS0tLS0tLS0tCiAgICAgICAgIDEgSG9ybmV0ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICAyIENCUiAxMDAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgMyBSMSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDQgQ0cxMjUgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA1IENHMTUwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNiBDQjMwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDcgRmF6ZXIgNjAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAoKNyBsaW5oYXMgc2VsZWNpb25hZGFzLg==” hover_enabled=”0″]U1FMPiBTRUxFQ1QgKgogIDIgIEZST00gbW90bzI7CgogICBNT1RPX0lEIE1PVE9fTk9NRSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIERPTk8KLS0tLS0tLS0tLSAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAtLS0tLS0tLS0tCiAgICAgICAgIDEgSG9ybmV0ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICAyIENCUiAxMDAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgMyBSMSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDQgQ0cxMjUgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA1IENHMTUwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNiBDQjMwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDcgRmF6ZXIgNjAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAoKNyBsaW5oYXMgc2VsZWNpb25hZGFzLg==[/et_pb_dmb_code_snippet]

Você pode estar se perguntando: fiz esse comando enorme apenas para deixar as tabelas iguais? Calma! Como a tabela moto2 estava vazia, sim, ele fez isso. Usando o mesmo comando MERGE novamente, ele fará as alterações de forma incremental. Vamos pra mais um exemplo, na sequência do que fizemos acima:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”U1FMPiBVUERBVEUgbW90bwogIDIgIFNFVCBkb25vID0gMTAxCiAgMyAgV0hFUkUgbW90b19pZCA9IDY7CjEgbGluaGEgYXR1YWxpemFkYS4KClNRTD4gSU5TRVJUIElOVE8gbW90bwogIDIgIChtb3RvX2lkLCBtb3RvX25vbWUsIGRvbm8pCiAgMyAgVkFMVUVTCiAgNCAgKDgsICdOaW5qYSAxMDAwJywgMTAyKTsKMSBsaW5oYSBjcmlhZGEuCgpTUUw+IGNvbW1pdDsKQ29tbWl0IGNvbmNsdcOtZG8u” hover_enabled=”0″]U1FMPiBVUERBVEUgbW90bwogIDIgIFNFVCBkb25vID0gMTAxCiAgMyAgV0hFUkUgbW90b19pZCA9IDY7CjEgbGluaGEgYXR1YWxpemFkYS4KClNRTD4gSU5TRVJUIElOVE8gbW90bwogIDIgIChtb3RvX2lkLCBtb3RvX25vbWUsIGRvbm8pCiAgMyAgVkFMVUVTCiAgNCAgKDgsICdOaW5qYSAxMDAwJywgMTAyKTsKMSBsaW5oYSBjcmlhZGEuCgpTUUw+IGNvbW1pdDsKQ29tbWl0IGNvbmNsdcOtZG8u[/et_pb_dmb_code_snippet]

Alteramos a tabela moto, agora vamos consultar os dados das 2 tabelas, para ver as diferenças:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”U1FMPiBTRUxFQ1QgKiBGUk9NIG1vdG87CgogICBNT1RPX0lEIE1PVE9fTk9NRSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIERPTk8KLS0tLS0tLS0tLSAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAtLS0tLS0tLS0tCiAgICAgICAgIDEgSG9ybmV0ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICAyIENCUiAxMDAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgMyBSMSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDQgQ0cxMjUgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA1IENHMTUwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNiBDQjMwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAxCiAgICAgICAgIDcgRmF6ZXIgNjAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA4IE5pbmphIDEwMDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDIKOCBsaW5oYXMgc2VsZWNpb25hZGFzLgoKU1FMPiBTRUxFQ1QgKiBGUk9NIG1vdG8yOwoKICAgTU9UT19JRCBNT1RPX05PTUUgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBET05PCi0tLS0tLS0tLS0gLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0gLS0tLS0tLS0tLQogICAgICAgICAxIEhvcm5ldCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgMiBDQlIgMTAwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDMgUjEgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA0IENHMTI1ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNSBDRzE1MCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDYgQ0IzMDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA3IEZhemVyIDYwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKNyBsaW5oYXMgc2VsZWNpb25hZGFzLg==” hover_enabled=”0″]U1FMPiBTRUxFQ1QgKiBGUk9NIG1vdG87CgogICBNT1RPX0lEIE1PVE9fTk9NRSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIERPTk8KLS0tLS0tLS0tLSAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAtLS0tLS0tLS0tCiAgICAgICAgIDEgSG9ybmV0ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICAyIENCUiAxMDAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgMyBSMSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDQgQ0cxMjUgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA1IENHMTUwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNiBDQjMwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAxCiAgICAgICAgIDcgRmF6ZXIgNjAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA4IE5pbmphIDEwMDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDIKOCBsaW5oYXMgc2VsZWNpb25hZGFzLgoKU1FMPiBTRUxFQ1QgKiBGUk9NIG1vdG8yOwoKICAgTU9UT19JRCBNT1RPX05PTUUgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBET05PCi0tLS0tLS0tLS0gLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0gLS0tLS0tLS0tLQogICAgICAgICAxIEhvcm5ldCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgMiBDQlIgMTAwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDMgUjEgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA0IENHMTI1ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNSBDRzE1MCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDYgQ0IzMDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA3IEZhemVyIDYwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKNyBsaW5oYXMgc2VsZWNpb25hZGFzLg==[/et_pb_dmb_code_snippet]

A diferença das duas tabelas são apenas 2 registros: o de moto_id = 6, onde o campo “dono” está diferente, e o moto_id = 8, que não existe na tabela moto2. Agora vamos rodar o MERGE novamente:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”U1FMPiBNRVJHRSBJTlRPIG1vdG8yIGEKICAyICAgIFVTSU5HIG1vdG8gYgogIDMgICAgICBPTiAoYS5tb3RvX2lkID0gYi5tb3RvX2lkKQogIDQgICAgV0hFTiBNQVRDSEVEIFRIRU4KICA1ICAgICAgVVBEQVRFIFNFVCBhLmRvbm8gPSBiLmRvbm8KICA2ICAgIFdIRU4gTk9UIE1BVENIRUQgVEhFTgogIDcgICAgICBJTlNFUlQgKG1vdG9faWQsIG1vdG9fbm9tZSwgZG9ubykKICA4ICAgICAgVkFMVUVTIChiLm1vdG9faWQsIGIubW90b19ub21lLCBiLmRvbm8pOwoKOCBsaW5oYXMgaW50ZXJjYWxhZGFzLgoKU1FMPiBjb21taXQ7CgpDb21taXQgY29uY2x1w61kby4KClNRTD4gU0VMRUNUICogRlJPTSBtb3RvMjsKCiAgIE1PVE9fSUQgTU9UT19OT01FICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgRE9OTwotLS0tLS0tLS0tIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIC0tLS0tLS0tLS0KICAgICAgICAgMSBIb3JuZXQgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDIgQ0JSIDEwMDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICAzIFIxICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNCBDRzEyNSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDUgQ0cxNTAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA2IENCMzAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDEKICAgICAgICAgNyBGYXplciA2MDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDggTmluamEgMTAwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMgoKOCBsaW5oYXMgc2VsZWNpb25hZGFzLg==” hover_enabled=”0″]U1FMPiBNRVJHRSBJTlRPIG1vdG8yIGEKICAyICAgIFVTSU5HIG1vdG8gYgogIDMgICAgICBPTiAoYS5tb3RvX2lkID0gYi5tb3RvX2lkKQogIDQgICAgV0hFTiBNQVRDSEVEIFRIRU4KICA1ICAgICAgVVBEQVRFIFNFVCBhLmRvbm8gPSBiLmRvbm8KICA2ICAgIFdIRU4gTk9UIE1BVENIRUQgVEhFTgogIDcgICAgICBJTlNFUlQgKG1vdG9faWQsIG1vdG9fbm9tZSwgZG9ubykKICA4ICAgICAgVkFMVUVTIChiLm1vdG9faWQsIGIubW90b19ub21lLCBiLmRvbm8pOwoKOCBsaW5oYXMgaW50ZXJjYWxhZGFzLgoKU1FMPiBjb21taXQ7CgpDb21taXQgY29uY2x1w61kby4KClNRTD4gU0VMRUNUICogRlJPTSBtb3RvMjsKCiAgIE1PVE9fSUQgTU9UT19OT01FICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgRE9OTwotLS0tLS0tLS0tIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIC0tLS0tLS0tLS0KICAgICAgICAgMSBIb3JuZXQgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDIgQ0JSIDEwMDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICAzIFIxICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNCBDRzEyNSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDUgQ0cxNTAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA2IENCMzAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDEKICAgICAgICAgNyBGYXplciA2MDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDggTmluamEgMTAwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMgoKOCBsaW5oYXMgc2VsZWNpb25hZGFzLg==[/et_pb_dmb_code_snippet]

O comando MERGE atualizou os dados da tabela moto2 conforme o especificado, ou seja, o registro que não existia foi inserido. E o registro que estava diferente foi alterado. Importante ver que o comando só especificou UPDATE no campo dono, portanto, se eu alterar o campo moto_nome na tabela moto, essa alteração não será refletida na tabela moto2 com o comando MERGE.

Outra cláusula que pode ser usada é a DELETE. Vamos preparar o caso para testar essa cláusula:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”U1FMPiBVUERBVEUgbW90bwogIDIgIFNFVCBkb25vID0gbnVsbAogIDMgIFdIRVJFIG1vdG9faWQgPSA3OwoKMSBsaW5oYSBhdHVhbGl6YWRhLgoKU1FMPiBJTlNFUlQgSU5UTyBtb3RvCiAgMiAgKG1vdG9faWQsIG1vdG9fbm9tZSwgZG9ubykKICAzICBWQUxVRVMKICA0ICAoOSwgJ05pbmphIDEwMDAnLCAxMDIpOwoKMSBsaW5oYSBjcmlhZGEuCgpTUUw+IFVQREFURSBtb3RvCiAgMiAgU0VUIGRvbm8gPSAxMDMKICAzICBXSEVSRSBtb3RvX2lkID0gNTsKCjEgbGluaGEgYXR1YWxpemFkYS4=” hover_enabled=”0″]U1FMPiBVUERBVEUgbW90bwogIDIgIFNFVCBkb25vID0gbnVsbAogIDMgIFdIRVJFIG1vdG9faWQgPSA3OwoKMSBsaW5oYSBhdHVhbGl6YWRhLgoKU1FMPiBJTlNFUlQgSU5UTyBtb3RvCiAgMiAgKG1vdG9faWQsIG1vdG9fbm9tZSwgZG9ubykKICAzICBWQUxVRVMKICA0ICAoOSwgJ05pbmphIDEwMDAnLCAxMDIpOwoKMSBsaW5oYSBjcmlhZGEuCgpTUUw+IFVQREFURSBtb3RvCiAgMiAgU0VUIGRvbm8gPSAxMDMKICAzICBXSEVSRSBtb3RvX2lkID0gNTsKCjEgbGluaGEgYXR1YWxpemFkYS4=[/et_pb_dmb_code_snippet]

Conferindo novamente os dados das duas tabelas:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”U1FMPiBTRUxFQ1QgKiBGUk9NIG1vdG87CgogICBNT1RPX0lEIE1PVE9fTk9NRSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIERPTk8KLS0tLS0tLS0tLSAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAtLS0tLS0tLS0tCiAgICAgICAgIDEgSG9ybmV0ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICAyIENCUiAxMDAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgMyBSMSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDQgQ0cxMjUgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA1IENHMTUwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDMKICAgICAgICAgNiBDQjMwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAxCiAgICAgICAgIDcgRmF6ZXIgNjAwCiAgICAgICAgIDggTmluamEgMTAwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMgogICAgICAgICA5IE5pbmphIDEwMDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDIKCjkgbGluaGFzIHNlbGVjaW9uYWRhcy4KClNRTD4gU0VMRUNUICogRlJPTSBtb3RvMjsKCiAgIE1PVE9fSUQgTU9UT19OT01FICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgRE9OTwotLS0tLS0tLS0tIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIC0tLS0tLS0tLS0KICAgICAgICAgMSBIb3JuZXQgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDIgQ0JSIDEwMDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICAzIFIxICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNCBDRzEyNSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDUgQ0cxNTAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA2IENCMzAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDEKICAgICAgICAgNyBGYXplciA2MDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDggTmluamEgMTAwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMgoKOCBsaW5oYXMgc2VsZWNpb25hZGFzLg==” hover_enabled=”0″]U1FMPiBTRUxFQ1QgKiBGUk9NIG1vdG87CgogICBNT1RPX0lEIE1PVE9fTk9NRSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIERPTk8KLS0tLS0tLS0tLSAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAtLS0tLS0tLS0tCiAgICAgICAgIDEgSG9ybmV0ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICAyIENCUiAxMDAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgMyBSMSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDQgQ0cxMjUgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA1IENHMTUwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDMKICAgICAgICAgNiBDQjMwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAxCiAgICAgICAgIDcgRmF6ZXIgNjAwCiAgICAgICAgIDggTmluamEgMTAwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMgogICAgICAgICA5IE5pbmphIDEwMDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDIKCjkgbGluaGFzIHNlbGVjaW9uYWRhcy4KClNRTD4gU0VMRUNUICogRlJPTSBtb3RvMjsKCiAgIE1PVE9fSUQgTU9UT19OT01FICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgRE9OTwotLS0tLS0tLS0tIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIC0tLS0tLS0tLS0KICAgICAgICAgMSBIb3JuZXQgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDIgQ0JSIDEwMDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICAzIFIxICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNCBDRzEyNSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDUgQ0cxNTAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA2IENCMzAwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDEKICAgICAgICAgNyBGYXplciA2MDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDggTmluamEgMTAwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMgoKOCBsaW5oYXMgc2VsZWNpb25hZGFzLg==[/et_pb_dmb_code_snippet]

E finalmente o comando MERGE usando a cláusula DELETE:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”U1FMPiBNRVJHRSBJTlRPIG1vdG8yIGEKICAyICAgIFVTSU5HIG1vdG8gYgogIDMgICAgICBPTiAoYS5tb3RvX2lkID0gYi5tb3RvX2lkKQogIDQgICAgV0hFTiBNQVRDSEVEIFRIRU4KICA1ICAgICAgVVBEQVRFIFNFVCBhLmRvbm8gPSBiLmRvbm8KICA2ICAgICAgV0hFUkUgIGIuZG9ubyBJUyBOT1QgTlVMTAogIDcgICAgREVMRVRFIFdIRVJFIChiLmRvbm8gPSAxMDIpOwoKNyBsaW5oYXMgaW50ZXJjYWxhZGFzLgoKU1FMPiBTRUxFQ1QgKiBGUk9NIG1vdG8yOwoKICAgTU9UT19JRCBNT1RPX05PTUUgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBET05PCi0tLS0tLS0tLS0gLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0gLS0tLS0tLS0tLQogICAgICAgICAxIEhvcm5ldCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgMiBDQlIgMTAwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDMgUjEgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA0IENHMTI1ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNSBDRzE1MCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAzCiAgICAgICAgIDYgQ0IzMDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMQogICAgICAgICA3IEZhemVyIDYwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKCjcgbGluaGFzIHNlbGVjaW9uYWRhcy4=” hover_enabled=”0″]U1FMPiBNRVJHRSBJTlRPIG1vdG8yIGEKICAyICAgIFVTSU5HIG1vdG8gYgogIDMgICAgICBPTiAoYS5tb3RvX2lkID0gYi5tb3RvX2lkKQogIDQgICAgV0hFTiBNQVRDSEVEIFRIRU4KICA1ICAgICAgVVBEQVRFIFNFVCBhLmRvbm8gPSBiLmRvbm8KICA2ICAgICAgV0hFUkUgIGIuZG9ubyBJUyBOT1QgTlVMTAogIDcgICAgREVMRVRFIFdIRVJFIChiLmRvbm8gPSAxMDIpOwoKNyBsaW5oYXMgaW50ZXJjYWxhZGFzLgoKU1FMPiBTRUxFQ1QgKiBGUk9NIG1vdG8yOwoKICAgTU9UT19JRCBNT1RPX05PTUUgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBET05PCi0tLS0tLS0tLS0gLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0gLS0tLS0tLS0tLQogICAgICAgICAxIEhvcm5ldCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgMiBDQlIgMTAwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAwCiAgICAgICAgIDMgUjEgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMAogICAgICAgICA0IENHMTI1ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKICAgICAgICAgNSBDRzE1MCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMTAzCiAgICAgICAgIDYgQ0IzMDAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDEwMQogICAgICAgICA3IEZhemVyIDYwMCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAxMDAKCjcgbGluaGFzIHNlbGVjaW9uYWRhcy4=[/et_pb_dmb_code_snippet]

O MERGE atualizou o registro 5, atualizando o campo dono para o valor 103, e também apagou os registros 8 e 9, obedecendo a condição especificada (dono = 102).