Quick actions

cmd+k|ctrl+k

Navigation

Languages

d

Snippet info

Language

C

Visibility

public

Author

sriram

Created

2023-11-14T08:08:36.467613Z

Updated

2023-11-14T08:08:36.467613Z

DECLARE
 no NUMBER := 0;
BEGIN
WHILE no < 10 LOOP
 no := no + 1;
 END LOOP;
 DBMS_OUTPUT.PUT_LINE('Sum :' || no);
END;

OUTPUT  : 
Statement processed.
Sum :10

FOR LOOP : 

BEGIN
FOR no IN 1 .. 5 LOOP
 DBMS_OUTPUT.PUT_LINE('Iteration : ' || no);
 END LOOP;
END;

OUTPUT : 
Statement processed.
Iteration : 1
Iteration : 2
Iteration : 3
Iteration : 4
Iteration : 5

7B . Exception Handling : 

create table customers(
    id int,
    name varchar(20),
    address varchar(20)
);

insert into customers values(1,'suresh','hyderabad');
insert into customers values(2,'Naresh','banglore');
insert into customers values(3,'Ramesh','chennai');
insert into customers values(4,'Pavitra','hyderabad');
insert into customers values(5,'Jessy','delhi');

DECLARE
 c_id customers.id%type := 8;
 c_name customers.name%type;
 c_addr customers.address%type;
BEGIN
 SELECT name, address INTO c_name, c_addr
 FROM customers
 WHERE id = c_id;
 DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
 DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
EXCEPTION
 WHEN no_data_found THEN
 dbms_output.put_line('No such customer!');
 WHEN others THEN
 dbms_output.put_line('Error!');
END;


Output : 

Statement processed.
No such customer!

Experiment 7C : 

DECLARE
  c_id customers.id%type := 2;
  c_name customers.name%type;
  c_addr customers.address%type;
  -- user-defined exception
  ex_invalid_id EXCEPTION;
BEGIN

  IF c_id <= 0 THEN
    RAISE ex_invalid_id;
  ELSE
    SELECT name, address INTO c_name, c_addr
    FROM customers
    WHERE id = c_id;
    DBMS_OUTPUT.PUT_LINE('Name: ' || c_name);
    DBMS_OUTPUT.PUT_LINE('Address: ' || c_addr);
  END IF;
EXCEPTION
  WHEN ex_invalid_id THEN
    dbms_output.put_line('ID must be greater than zero!');
  WHEN no_data_found THEN
    dbms_output.put_line('No such customer!');
  WHEN others THEN
    dbms_output.put_line('Error!');
END;


output : 

Statement processed.
Name: Naresh
Address: banglore


INFO