Sie sind auf Seite 1von 4

Exercise – 10

1.)
PL/SQL Cursors
Create a table Student (regno, name, dept, cgpa) with minimum five
records
1. Create an Explicit Cursor to list all the Student
having cgpa >9.0 and transfer it to a new table.

SQL> set serveroutput on;


SQL> declare
2 a_name student.name%type;
3 cursor try is
4 select name from Student_info where cgpa>9.0;
5 begin
6 open try;
7 fetch try into a_name;
8 insert into name values(a_name);
9 dbms_output.put_line(a_name);
10 close try;
11 end;
12 /
andiappan

PL/SQL procedure successfully completed.

SQL> select * from name;

NAME
--------------------
andiappan
2.)
Write an Implicit Cursor to display the details of
CSE student.

SQL> declare
2 a_name Student_info.name%type;
3 a_regno Student_info.regno%type;
4 a_dept Student_info.dept%type;
5 a_cgpa Student_info.cgpa%type;
6 cursor try_2 is
7 select name,regno,dept,cgpa from Student_info where dept='cse';
8 begin
9 open try_2;
10 fetch try_2 into a_name,a_regno,a_dept,a_cgpa;
11 insert into name_2 values(a_name,a_regno,a_dept,a_cgpa);
12 dbms_output.put_line(a_name||''||a_regno||''||a_dept||''||a_cgpa);
13 close try_2;
14 end;
15 /
vasanth1006cse9.2

PL/SQL procedure successfully completed.

SQL> select * from name_2;

NAME REGNO DEPT CGPA


------------------------------------
vasanth 1006 cse 9.2

PL/SQL Triggers
1. Considering the above relation, raise a trigger,
when the cgpa exceeds 9.0 or goes below 4.0.
SQL> create trigger cg
2 before insert on student
3 for each row
4 when(NEW.cgpa>9.0 or NEW.cgpa<4.0)
5 begin
6 if(NEW.cgpa>9.0)
7 dbms_output.putline('cgpa below 9.0');
8 then
9 dbms_output.putline('cgpa below 4.0');
10 end if;
11 end;
12 /

SQL>Trigger created

SQL> insert into student values(1001,'teja','cse',9.5);

1 row created.

2. The table PRODUCTS has 2 columns, PRO_ID and


PRICE.
Create the trigger PRO_TRG that raises the application
error 'Price cannot be changed by more than 15%’ when
an update statement tries to change price by more than
15 percent.

create trigger pro_tag


2 before update on products
3 for each row
4 when(NEW.price>(OLD.price+(0.15*OLD.price)))
5 begin
6 dbms_output.put_line('price cannot be changed by more than
15%');
7 end
8 /

Trigger created

SQL> insert into products values(1547,100);

1 row created.

Das könnte Ihnen auch gefallen