0% found this document useful (0 votes)
13 views5 pages

Week 5

The document contains a series of PL/SQL programs demonstrating basic operations such as outputting text, performing arithmetic calculations, creating and manipulating tables, and handling exceptions. It includes examples of creating a student marks table, inserting records, and selecting students based on their percentage. Additionally, it showcases the use of savepoints, commits, and rollbacks in database transactions.

Uploaded by

jahnavisindhu673
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Week 5

The document contains a series of PL/SQL programs demonstrating basic operations such as outputting text, performing arithmetic calculations, creating and manipulating tables, and handling exceptions. It includes examples of creating a student marks table, inserting records, and selecting students based on their percentage. Additionally, it showcases the use of savepoints, commits, and rollbacks in database transactions.

Uploaded by

jahnavisindhu673
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Exp No:

Date: Page No:

Week-5
Program:
Sample PL/SQL program:
SQL> begin
2 dbms_output.put_line('hello');

3 end;

4/

OUTPUT:
hello
PL/SQL procedure successfully completed.
Sum of two numbers:
SQL> declare
2 a integer;

3 b integer;

4 c integer;

5 begin

6 a:=2;

7 b:=3;

8 c:=a+b;

9 dbms_output.put_line('sum of'||a||'and'||b||'is'||c);

10 end;

11 /
OUTPUT:
sum of2and3is5
PL/SQL procedure successfully completed.

SQL> create table student_marks (


2 STUDENT_ID NUMBER PRIMARY KEY,
3 STUDENT_NAME VARCHAR2(50),
4 TOTAL_MARKS NUMBER(5),
5 PERCENTAGE NUMBER(5,2) );
OUTPUT:

Table created.
SQL> insert into student_marks values(101,'ravi',560,70);

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

OUTPUT:
1 row created.
SQL> select * from student_marks;
OUTPUT:
STUDENT_ID STUDENT_NAME TOTAL_MARKS PERCENTAGE

101 ravi 56 70

102 Anil 450 20

103 Balu 620 78

104 Charan 580 72

105 David 390 48

Program for Student marks can be selected from the table and printed for who secured
first class and an exception can be raised if no records were found.
SQL>DECLARE
2 scountnumber;

3 no_records_found EXCEPTION;

4 BEGIN

5 select count(*) into scount from student_marks where percentage >=60;

6 if scount =0 then

7 RAISE no_records_found;

8 end if;

9 dbms_output.put_line('first class students are ------ ::');

10 for records in (select * from student_marks where percentage >=60)

11 loop

12 dbms_output.put_line(records.student_id);

13 end loop;

14 EXCEPTION

15 when no_records_found then

16 dbms_output.put_line('No students secured first class.');

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

17 when others then

18 dbms_output.put_line('An unexpected error occured');

19 END;

20 /
OUTPUT:
first class students are-----::
101
103
104
PL/SQL procedure successfully completed.

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

Program:5B
SQL> create table student
2(
3 rollno integer,
4 name varchar2(20) );

OUTPUT:
Table created.

SQL> insert into student values(501,'ravi');


Output:
1 row created.
SQL> savepoint a;
Output:
Savepoint created.
SQL> insert into student values(502,'suma');
Output:
1 row created.
SQL> savepoint b;
Output:
Savepoint created.
SQL> insert into student values(503,'padma');
Output:
1 row created.
SQL> select * from student;
Output:
ROLLNO NAME
------------ ---------------
501 ravi
502 suma
503 padma

SQL> rollback to b;

Output:
Rollback complete.

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543
Exp No:
Date: Page No:

SQL> select * from student;


Output:
ROLLNO NAME
------------ ---------------
501 ravi
502 suma
SQL> rollback to a;
Output:
ROLLNO NAME
------------ ---------------
501 ravi
Program using savepoint , commit and rollback:
SQL> begin
1. insert into student values(504,'yashoda');
2. update student set name='rani' where rollno=504;
3. savepoint s;
4. delete from student where rollno=501;
5. rollback to s;
6. commit;
7. end;
8. /
Output:
PL/SQL procedure successfully completed.
SQL> select * from student;
Output:
ROLLNO NAME
---------- --------------------
501 ravi
504 rani

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0543

You might also like