PL/SQL

Hands On No. 2 : Introduction to PL/SQL Block

Basic Structure of PL/SQL Block
DECLARE  -- Declarative part (optional)
 -- Declarations of local types, variables, & subprograms

BEGIN  -- Executable part (required)
 -- Statements (which can use items declared in declarative part)
DBMS_OUTPUT.PUT_LINE ('Hello World');

EXCEPTION  -- Exception-handling part (optional)
    WHEN OTHERS THEN 
        DBMS_OUTPUT.PUT_LINE ('Error');
        -- Exception handlers for exceptions raised in executable part]
END;
                                    
Example of PL/SQL Block

Definition : Display the Sum of Two numbers

DECLARE  -- Declarative part (optional)
 -- Declarations of local types, variables, & subprograms
 
aa number(4);
bb number(4);
cc number(8);

BEGIN  -- Executable part (required)
 -- Statements (which can use items declared in declarative part)
 aa:=10;
 bb:=20;
 cc:= aa+bb;
DBMS_OUTPUT.PUT_LINE('Sum is '||cc);

EXCEPTION  -- Exception-handling part (optional)
    WHEN OTHERS THEN 
        DBMS_OUTPUT.PUT_LINE ('Error');
        -- Exception handlers for exceptions raised in executable part]
END;
                                    

Now in above example change the following code and check the output.

bb:=0; 
cc:= aa/bb;