The PL/SQL anonymous block statement is an executable statement that can contain PL/SQL control statements and SQL statements. It can be used to implement procedural logic in a scripting language. In PL/SQL contexts, this statement can be compiled and executed by the DB2® data server.
The anonymous block statement, which does not persist in the database, can consist of up to three sections: an optional declaration section, a mandatory executable section, and an optional exception section.
The optional declaration section, which can contain the declaration of variables, cursors, and types that are to be used by statements within the executable and exception sections, is inserted before the executable BEGIN-END block.
The optional exception section can be inserted near the end of the BEGIN-END block. The exception section must begin with the keyword EXCEPTION, and continues until the end of the block in which it appears.
This statement can be executed from an interactive tool or command line interface such as the CLP. This statement can also be embedded within a PL/SQL procedure definition, function definition, or trigger definition. Within these contexts, the statement is called a block structure instead of an anonymous block statement.
No privileges are required to invoke an anonymous block. However, the privileges held by the authorization ID of the statement must include all necessary privileges to invoke the SQL statements that are embedded within the anonymous block.
.-----------. V | >>-+------------------------------+--BEGIN----statement-+-------> | .-------------. | | V | | '-+---------+----declaration-+-' '-DECLARE-' >--+-----------------------------------------------------------------------------------+--> | .--------------------------------------------------------------------. | | | .-----------------------------. .-------------------. | | | V V | V | | | '-EXCEPTION----WHEN----exception-condition--+----+-+--THEN----handler-statement-+-+-' '-OR-' >--END---------------------------------------------------------><
BEGIN
NULL;
END;
SET SERVEROUTPUT ON;
BEGIN
dbms_output.put_line( 'Hello' );
END;
SET SERVEROUTPUT ON;
DECLARE
current_date DATE := SYSDATE;
BEGIN
dbms_output.put_line( current_date );
END;