Most Trivial Block Ever
BEGIN
NULL;
END;
Statement processed.
Hello World!
BEGIN
DBMS_OUTPUT.put_line ('Hello World');
END;
Hello World
Block With Declaration Section
DECLARE
c_message CONSTANT VARCHAR2 (100) := 'Hello World';
BEGIN
DBMS_OUTPUT.put_line (c_message);
END;
Hello World
Block With Declaration and Exception Sections
DECLARE
l_message VARCHAR2 (10);
BEGIN
l_message := 'Hello World';
DBMS_OUTPUT.put_line (l_message);
EXCEPTION
WHEN VALUE_ERROR
THEN
DBMS_OUTPUT.put_line ('Variable too small!');
RAISE;
END;
ORA-06502: PL/SQL: numeric or value error: character string buffer too small ORA-06512: at line 10 ORA-06512: at line 4 ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-06502
Nested Blocks
BEGIN
BEGIN
DBMS_OUTPUT.put_line ('Hello World');
END;
BEGIN
DBMS_OUTPUT.put_line ('Hello SQL');
END;
END;
Variable too small!
Hello World
Hello SQL
Front-Loaded Declarations
DECLARE
l_message VARCHAR2 (100) := 'Hello World';
l_message2 VARCHAR2 (10);
BEGIN
FOR indx IN 1 .. 5
LOOP
l_message := l_message || ' and ' || indx;
END LOOP;
DBMS_OUTPUT.put_line (l_message);
BEGIN
l_message2 := SUBSTR (l_message, 1, 10);
DBMS_OUTPUT.put_line (l_message2);
END;
END;
Hello World and 1 and 2 and 3 and 4 and 5
Hello Worl
Just In Time Declarations
DECLARE
l_message VARCHAR2 (100) := 'Hello World';
BEGIN
FOR indx IN 1 .. 5
LOOP
l_message := l_message || ' and ' || indx;
END LOOP;
DBMS_OUTPUT.put_line (l_message);
DECLARE
l_message2 VARCHAR2 (100);
BEGIN
l_message2 := SUBSTR (l_message, 1, 100);
DBMS_OUTPUT.put_line (l_message2);
END;
END;
Hello World and 1 and 2 and 3 and 4 and 5
Hello World and 1 and 2 and 3 and 4 and 5
Nested Block Constrains Exception Propagation
DECLARE
l_message VARCHAR2 (100) := 'Hello World';
BEGIN
FOR indx IN 1 .. 5
LOOP
l_message := l_message || ' and ' || indx;
END LOOP;
DBMS_OUTPUT.put_line (l_message);
DECLARE
l_message2 VARCHAR2 (10);
BEGIN
l_message2 := SUBSTR (l_message, 1, 100);
DBMS_OUTPUT.put_line (l_message2);
EXCEPTION
WHEN VALUE_ERROR
THEN
/* OK, that failed, but I can continue if I want */
DBMS_OUTPUT.put_line ('Variable too small!');
END;
FOR indx IN 1 .. 5
LOOP
l_message := indx || ' and ' || l_message;
END LOOP;
DBMS_OUTPUT.put_line (l_message);
END;
Hello World and 1 and 2 and 3 and 4 and 5
Variable too small!
5 and 4 and 3 and 2 and 1 and Hello World and 1 and 2 and 3 and 4 and 5
Nested Subprogram Instead of Block
DECLARE
l_message VARCHAR2 (100) := 'Hello SQL';
PROCEDURE with_message2
IS
l_message2 VARCHAR2 (10);
BEGIN
l_message2 := SUBSTR (l_message, 1, 100);
DBMS_OUTPUT.put_line (l_message2);
EXCEPTION
WHEN VALUE_ERROR
THEN
/* OK, that failed, but I can continue if I want */
DBMS_OUTPUT.put_line ('Variable too small!');
END;
BEGIN
FOR indx IN 1 .. 5
LOOP
l_message := l_message || ' and ' || indx;
END LOOP;
DBMS_OUTPUT.put_line (l_message);
with_message2;
FOR indx IN 1 .. 5
LOOP
l_message := indx || ' and ' || l_message;
END LOOP;
DBMS_OUTPUT.put_line (l_message);
END;
Hello SQL and 1 and 2 and 3 and 4 and 5
Variable too small!
5 and 4 and 3 and 2 and 1 and Hello SQL and 1 and 2 and 3 and 4 and 5