Check for UK before creating a

Asked

Viewed 82 times

0

I have the following query:

alter table FINALIDADE_OPERACAO_MODAL 
        add constraint UK_t127pwh154arjh5whq0g4dlrm unique (NOME);

As I create a function that checks if it exists before, and if it does not exist, creates, and if it already exists, it exits the command.

1 answer

0

Without getting into the merits of the solution

declare
    vn_qtd number;
    ...
    begin
      SELECT count(*)
      into vn_qtd
      FROM DBA_CONSTRAINTS 
      WHERE CONSTRAINT_NAME = 'UK_t127pwh154arjh5whq0g4dlrm';
      if vn_qtd = 0 then
        execute_immediate ('alter table FINALIDADE_OPERACAO_MODAL ' ||
                           ' add constraint UK_t127pwh154arjh5whq0g4dlrm unique (NOME)');
      end if;
end;

Browser other questions tagged

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