Oracle Anonymous Block

Oracle Anonymous Block

The Oracle anonymous block consists of the following sections:
The declarative section is the place where variables, cursors, object types are declared.
The execution section is the place for SQL statements or PL/SQL statements like: select, insert, update, delete, create, drop, alter, loops.
The exception section is the place for error handling.

Oracle Anonymous block syntax

DECLARE -- Declarative section (optional)
-- Variable
BEGIN -- Execution section (required)
-- Statements
EXCEPTION -- Exception section (optional)
-- Error handling

Anonymous block example

 v_name VARCHAR2(255);
 SELECT name
 INTO v_name
 FROM customers
 WHERE id=1;
 dbms_output.put_line('Customer: '||v_name);
 WHEN no_data_found THEN
 dbms_output.put_line('No customer found!');
  WHEN others THEN
 dbms_output.put_line('Error msg: '||sqlerrm);