Block Variable Visibility - Exercise

Contact Us or call 1-877-932-8228
Block Variable Visibility - Exercise

Block Variable Visibility

Duration: 10 to 15 minutes.
  1. Create a block that contains a declaration of a numerical variable named x.
  2. Retrieve a count of records in the employees table into x.
  3. Create a inner block with a declaration section where a second numeric variable named x is declared.
  4. Within the inner block, retrieve a count of records in the departments table into x.
  5. Display the value of x (containing the count of records in the departments table).
  6. Within the outer block (following the inner block) display the number of records in the employees table (retrieved into x in the outer block earlier).

Solution:

Nested-Blocks/Solutions/inner_outer_block.sql
declare
 x NUMBER;
begin
  SELECT count(*)
  into x
  from employees;
  
  declare
    x NUMBER;
  begin
    SELECT count(*)
    into x
    from departments;
    dbms_output.put_line('Departments ' || x);
  end;

  dbms_output.put_line('Employees ' || x);
end;
/

Code Explanation

Next