Display a message in the command output

1

I'm running this PL / SQL and if the condition drops to else , how to show the message to the user?

declare
  cont integer;
  tabela varchar2(100) := 'PESSOA';
  coluna varchar2(100) := 'NOME';
begin
    SELECT COUNT(NOME) into cont FROM PESSOA;
   if cont = 0 then
       execute immediate 'ALTER TABLE '||tabela||' DROP COLUMN '||coluna||'';
       commit;
   else
       ('Coluna '||coluna||'não excluído porque contém Dados');
   end if;
end;
/
    
asked by anonymous 13.12.2018 / 13:14

2 answers

2

To print to the console just use DBMS_OUTPUT.put_line :

begin
    DBMS_OUTPUT.put_line('Minha mensagem');
end;

In your code it would look like this:

declare
  cont integer;
  tabela varchar2(100) := 'PESSOA';
  coluna varchar2(100) := 'NOME';
begin
    SELECT COUNT(NOME) into cont FROM PESSOA;
   if cont = 0 then
       execute immediate 'ALTER TABLE '||tabela||' DROP COLUMN '||coluna||'';
       commit;
   else
       DBMS_OUTPUT.put_line('Coluna '||coluna||'não excluído porque contém Dados');
   end if;
end;

To see the output in SQL Developer go to: View - > DBMS Output

    
13.12.2018 / 13:21
1

Because the message refers to an error, you can use the Raise_application_error , which will interrupt the execution of the PLSQL procedure and raise an exception for the application that is connected to the database.

    
13.12.2018 / 13:21