0% found this document useful (0 votes)
39 views

DBMS Assignment - 3

The document discusses implementing various PL/SQL concepts like procedures, functions, views, exceptions, triggers and cursors. It provides code examples for a procedure and function to swap two values, a view to select student records, exception handling for division by zero, an audit trigger for an employee table and a cursor to iterate through records in a student table. The code examples are accompanied by output sections.

Uploaded by

parteek singhal
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)
39 views

DBMS Assignment - 3

The document discusses implementing various PL/SQL concepts like procedures, functions, views, exceptions, triggers and cursors. It provides code examples for a procedure and function to swap two values, a view to select student records, exception handling for division by zero, an audit trigger for an employee table and a cursor to iterate through records in a student table. The code examples are accompanied by output sections.

Uploaded by

parteek singhal
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/ 7

Mohan Aggarwal MCA – 1B

ASSIGNMENT – 3
IMPLEMENTING TRIGGERS, PROCEDURES, FUNCTIONS, VIEWS.
QUESTION 1: Write a procedure using pl/sql to implement swapping two values.
CODE:
CREATE OR REPLACE PROCEDURE SWAP_PROC(A IN OUT INTEGER, B IN OUT
INTEGER) AS
TEMP INTEGER;
BEGIN
TEMP := A;
A := B;
B := TEMP;
END;
/

DECLARE
X INTEGER := 5;
Y INTEGER := 7;
BEGIN
SWAP_PROC(X, Y);
DBMS_OUTPUT.PUT_LINE('X = ' || X || ', Y = ' || Y);
END;
/
Mohan Aggarwal MCA – 1B

OUTPUT:

QUESTION 2: Write a function to implement swap two values.


CODE:
CREATE OR REPLACE FUNCTION SWAP_FUNC(A IN OUT INTEGER, B IN OUT
INTEGER) RETURN VARCHAR IS
TEMP INTEGER;
BEGIN
TEMP := A;
A := B;
B := TEMP;
RETURN 'A = ' || A || ', B = ' || B;
END SWAP_FUNC;
/

DECLARE
A INTEGER := 5;
B INTEGER := 7;
STR VARCHAR(50);
Mohan Aggarwal MCA – 1B

BEGIN
STR := SWAP_FUNC(A, B);
DBMS_OUTPUT.PUT_LINE(STR);
DBMS_OUTPUT.PUT_LINE('A = ' || A);
DBMS_OUTPUT.PUT_LINE('B = ' || B);
END;
/

OUTPUT:

QUESTION 3: Implement the concept of views.


CODE:
CREATE VIEW DETAILS AS
SELECT NAME, ADDRESS
FROM STUDENT
WHERE SID < 5;
Mohan Aggarwal MCA – 1B

OUTPUT:

CODE:
SELECT * FROM DETAILS;

OUTPUT:
Mohan Aggarwal MCA – 1B

QUESTION 4: Implement the concept of exception handling.


CODE:
DECLARE
A INTEGER := 10;
B INTEGER := 0;
ANSWER INTEGER;
BEGIN
ANSWER := A/B;
DBMS_OUTPUT.PUT_LINE(TO_CHAR(ANSWER));

EXCEPTION WHEN ZERO_DIVIDE THEN


DBMS_OUTPUT.PUT_LINE('ERROR: DIVISION BY ZERO');
END;
/

OUTPUT:

QUESTION 5: Implement the audit trigger.


CODE:
Mohan Aggarwal MCA – 1B

CREATE TRIGGER EMP_UPDATE


BEFORE UPDATE ON EMP
FOR EACH ROW
BEGIN
IF UPDATING THEN
INSERT INTO AUDIT_EMP VALUES(:OLD.ENO, SYSDATE, 'UPDATE');
END IF;
END;
/

UPDATE EMP SET ENO = 5 WHERE ENO = 3;


SELECT * FROM AUDIT_EMP;

OUTPUT:

QUESTION 6: Implement one example of cursor.


CODE:
DECLARE
S_ID STUDENT.SID%TYPE;
Mohan Aggarwal MCA – 1B

S_NAME STUDENT.NAME%TYPE;
S_ADDR STUDENT.ADDRESS%TYPE;
CURSOR S_STUDENT IS
SELECT SID, NAME, ADDRESS FROM STUDENT;
BEGIN
OPEN S_STUDENT;
LOOP
FETCH S_STUDENT INTO S_ID, S_NAME, S_ADDR;
EXIT WHEN S_STUDENT%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(S_ID || ' ' || S_NAME || ' ' || S_ADDR);
END LOOP;
CLOSE S_STUDENT;
END;
/

OUTPUT:

You might also like