Anonymous block

What is Anonymous block?

  • Declarative  - Optional
  • Begin -  mandatory part ( two sub parts)

    • Executable (Mandatory )
    • Exception handlers. (Optional)

Anonymous block is a PL/SQL program unit that has no name. Anonymous block consists of below section

Declarative part :: declares PL/SQL variables, exceptions, and cursors. 
Begin - Executable :: part contains PL/SQL code and SQL statements, and can contain nested blocks. 
Begin - Exception :: handlers contain code that is used to handle any type of Exception  

Let's look at most simplest of block:
We put all the executable lines in side begin and end block. We must have atleast one statement inside this block.

CODE
BEGIN
  <valid statement>;
END;
/
BEGIN
  NULL;
END;
/

Below statement will raise error - at this needs atleast one valid statement
BEGIN
END;
/

 Sample anonymous blocks  

CODE
OUTPUT
BEGIN
  DBMS_OUTPUT.PUT_LINE(‘HELLO’);
END ;
/
HELLO
DECLARE
BEGIN
  DBMS_OUTPUT.PUT_LINE(‘HELLO’);
END ;
/
HELLO

Using of Declare in Block:
Declare is used to declare variable, cursors etc., 


CODE
OUTPUT
DECLARE – Optional
<<Variable, cursor declaration>>
BEGIN
  <<Executable statements>>
END ;
/
DECLARE
VAR1 varchar2(100);  -- Declaring Variable
BEGIN
Var1:= 101;  -- Assign value
  DBMS_OUTPUT.PUT_LINE(‘Value : ‘ ||  Var1);
END ;
/
Value : 101


In above code we can see variable declaration in Declare section while value is assigned in Begin section. 

<< Pointer  >>

  • We can use “||” double pipe operator two concatenate two values or string in Oracle.
  • Oracle is Case insensitive we can declare variable in Caps and use them in any case


CODE
OUTPUT
DECLARE

BEGIN

  DBMS_OUTPUT.PUT_LINE('Hello');

END ;

/
Hello

Declare block is optional statement once declared its not mandatory to have some statement as shown in below block we can find Declare statement but does  not contain any variable declaration


CODE
OUTPUT
DECLARE
Var1 varchar2(100):=101;   -- Declaration +   Assignment
BEGIN
  DBMS_OUTPUT.PUT_LINE(‘Value : ‘  ||  Var1);
END ;
/
Value : 101

In above section we have declaration and assignment in single statement this is allowed.


CODE
OUTPUT
DECLARE
VAR1 VARCHAR2(100); -- Declaration
var1:=101;   -- Assignment in Declare
BEGIN
  DBMS_OUTPUT.PUT_LINE('Value : ' ||  var1);
END ;
/
Error report:
ORA-06550:
PLS-00103: Encountered the symbol "=" when expecting …….

Declare block allows Declaration and Assignment together but we cannot split in two statements, this is not allowed and will raise an error.



CODE
OUTPUT
DECLARE
VAR1 VARCHAR2(100);  -- Declaration
BEGIN
VAR1:=101;   --  Assignment
  DBMS_OUTPUT.PUT_LINE('Value : ' ||  VAR1);
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('ERROR');
END ;
/
Value : 101












Error while performing invalid operation



CODE
OUTPUT
DECLARE
VAR1 NUMBER(10);  -- Declaration
BEGIN
VAR1:='ABC';   --  INVALID Assignment
END ;
/
ORA-06502: PL/SQL: numeric or value error: character to number conversion error











Block with Exception handling

CODE
OUTPUT
BEGIN
  <valid statement>;
Exception
  <Exception type>
  <On exception operation>
END;
/
BEGIN
  dbms_output.put_line ('test');
Exception
  When Others Then
     dbms_output.put_line ('Error');
END;
/

Declare – Begin – Exception

CODE
OUTPUT
DECLARE
VAR1 VARCHAR2(100);  -- Declaration
BEGIN
VAR1:=101;   --  Assignment
  DBMS_OUTPUT.PUT_LINE('Value : ' ||  VAR1);
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('ERROR');
END ;
/
Value : 101












Declare-Begin-Exception ::  with invalid assignment 

CODE
OUTPUT
DECLARE
VAR1 NUMBER(10);  -- Declaration
BEGIN
VAR1:='ABC';   --  INVALID Assignment
EXCEPTION    -- Error Handeling
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('ERROR');
END ;
/
ERROR












This is very similar to Try-Catch Statements in JAVA where on any type of error it goes to specific error block and program does not abruptly end.

Declare-Begin-Exception :: With use of SQLERRM

CODE
OUTPUT
DECLARE
VAR1 NUMBER(10);  -- Declaration
BEGIN
VAR1:='ABC';   --  INVALID Assignment
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE(SQLERRM);
END ;
//
ORA-06502: PL/SQL: numeric or value error: character to number conversion error











<< SQLERRM>> is predefined SQL variable with stores error encountered while execution this will help  us to understand what was the exact error. 


Anonymous block with Loop.

CODE
OUTPUT
DECLARE
  VAR1 VARCHAR2(10):='DEV';
BEGIN
FOR I in 1..5 loop
  DBMS_OUTPUT.PUT_LINE(I || '  ' || VAR1);
END LOOP ;
end ;
/
1  DEV
2  DEV
3  DEV
4  DEV
5  DEV

Nested Anonymous block:  It is possible to nest one block into another.
CODE
OUTPUT
BEGIN
FOR I IN 1..5 LOOP
      DECLARE
        VAR1 VARCHAR2(10):='DEV';
      begin
        DBMS_OUTPUT.PUT_LINE(I || '  ' || VAR1);
      end ;
END LOOP ;
END ;
/
1  DEV
2  DEV
3  DEV
4  DEV
5  DEV

Nested Anonymous block with same variable name:  It is possible to nest one block into another.
CODE
OUTPUT
DECLARE
        VAR1 VARCHAR2(10):='DEV';
BEGIN
  DBMS_OUTPUT.PUT_LINE(' Outer block before : ' || VAR1);
FOR I IN 1..5 LOOP
  DECLARE
   VAR1 VARCHAR2(10):='DEVANG';
  BEGIN
   DBMS_OUTPUT.PUT_LINE(I || ' inner   block  ' || VAR1);
      END ;
END LOOP ;
DBMS_OUTPUT.PUT_LINE(' Outer block After : ' || VAR1);
END ;
/
Outer block before : DEV
1 inner block  DEVANG
2 inner block  DEVANG
3 inner block  DEVANG
4 inner block  DEVANG
5 inner block  DEVANG
 Outer block After : DEV

We have used same variable name in outer block as well as inner block, this uses simple fundamental of overriding
Variable “VAR1” of inner block overrides the scope of Outer block as preference is given to  local variable.
Nested Anonymous block with variable scope :  It is possible to nest one block into another.
CODE
OUTPUT
DECLARE
oVAR  VARCHAR2(20):='DEVSHAH';
BEGIN
DECLARE
iVAR VARCHAR2(20):='SHAH';
BEGIN
DBMS_OUTPUT.PUT_LINE(' inner  block ‘);
DBMS_OUTPUT.PUT_LINE(‘Outer var ' || oVAR);
DBMS_OUTPUT.PUT_LINE(' inner  var' || iVAR);
END ;
DBMS_OUTPUT.PUT_LINE(' Outer  block ‘);
DBMS_OUTPUT.PUT_LINE(‘Outer var ' || oVAR);
--Variable “iVAR” not visible outside inner block
DBMS_OUTPUT.PUT_LINE(' inner  var' || iVAR); 
END ;
/
ERROR
PLS-00201: identifier 'IVAR' must be declared

This means scope of inner block is limited variable declared inside inner block are not visible outside the block.
Nested Anonymous block with variable scope :  It is possible to nest one block into another.
CODE
OUTPUT
DECLARE
oVAR  VARCHAR2(20):='DEVSHAH';
BEGIN
DECLARE
iVAR VARCHAR2(20):='SHAH';
BEGIN
DBMS_OUTPUT.PUT_LINE(' --Inner  block-- ‘);
DBMS_OUTPUT.PUT_LINE(‘Outer var ' || oVAR);
DBMS_OUTPUT.PUT_LINE(' inner  var' || iVAR);
END ;
DBMS_OUTPUT.PUT_LINE(' --Outer  block-- ‘);
DBMS_OUTPUT.PUT_LINE(‘Outer var ' || oVAR);
END ;
/
--Inner  block--
Outer block variable
Inner block Variable
-- Outer  block --
Outer block variable



No comments:

Post a Comment