How to insert data into another database table only if it does not exist?

Asked

Viewed 29 times

1

I would like it if, by inserting it in the Customers table in the bank 1, it were inserted in the Customers table in the bank 2, but only if there is no more. Currently my code is like this:

DELIMITER $$
CREATE TRIGGER trIntegracaoBancos AFTER INSERT
ON banco1.clientes FOR EACH ROW

BEGIN
    IF NOT EXISTS (SELECT id FROM banco2.Clientes) THEN
            INSERT INTO banco2.Clientes (id, nome, dat_nascimento, cpf, rg, endereco, numero, bairro, cidade, estado, cep)
            SELECT id, nome, dat_nascimento, cpf, rg, endereco, numero, bairro, cidade, estado, cep;
    END IF;
END;

$$

The if check should check if there is no.

  • 1

    " INSERT INTO products (name, category, description) VALUES ('Black Join', 'Electronics', 'Lorem Ipsum); " only inserts if it does not exist by default if it returns an error.

  • 1

    Do not edit your question with an answer. Please read the [Tour] and [Help] to better understand the site.

  • Bacco my later comment was not a comment, but a solution. I should read the content before making any decision.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.