DBMS PLSQL
DBMS PLSQL
DBMS PLSQL
As the name implies, PL/SQL supports programming language features like conditional
statements, iterative statements.The programming constructs are similar to how you use
in programming languages like Java and C++. In this section I will provide you syntax of
how to use conditional statements in PL/SQLprogramming.
Write a PL/SQL program to find the Arithimetic Operators by using if,else if,else
SET SERVEROUTPUT ON
DECLARE
a number(3):=&a;
b number(3):=&b;
ch number(3):=&ch;
r number(4);
BEGIN
IF ch=1 THEN
r:=a+b;
DBMS_OUTPUT.PUT_LINE('Addition : '||r);
1
ELSIF ch=2 THEN
r:=a-b;
DBMS_OUTPUT.PUT_LINE('Subtraction : '||r);
r:=a*b;
DBMS_OUTPUT.PUT_LINE('Multiplication : '||r);
r:=a/b;
DBMS_OUTPUT.PUT_LINE('Division : '||r);
r:=mod(a,b);
DBMS_OUTPUT.PUT_LINE('Remainder : '||r);
ELSE
END IF;
END;
Output:
Addition : 18
2
2)FOR Loop: A FOR LOOP is used to execute a set of statements for a pre-determined
number of times. Iteration occurs between the start and end integer values given. The
counter is always incremented by 1. The loop exits when the counter reaches the value of the
end integer.
Syntax:
FOR counter IN val1..val2
statements;
ENDLOOP
SET SERVEROUTPUT ON
DECLARE
n number(3):=&n;
BEGIN
FOR i IN 1..n
LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;
Output:
3
3)Triggers:Trigger is invoked by Oracle engine automatically whenever a specified event
occurs.Trigger is stored into database and invoked repeatedly, when specific condition match.
Triggers are stored programs, which are automatically executed or fired when some event occurs.
Triggers could be defined on the table, view, schema, or database with which the event is associated.
1. CREATE [OR REPLACE ] TRIGGER trigger_name
2. {BEFORE | AFTER | INSTEAD OF }
3. {INSERT [OR] | UPDATE [OR] | DELETE}
4. [OF col_name]
5. ON table_name
6. [REFERENCING OLD AS o NEW AS n]
7. [FOR EACH ROW]
8. WHEN (condition)
9. DECLARE
10. Declaration-statements
11. BEGIN
12. Executable-statements
13. EXCEPTION
14. Exception-handling-statements
15. END;
4
Create 2 Tables Student and StudentHistory Tables
Create Table Student( Sno number(5),Sname varchar2(12),Course varchar2(10));
Create Table StudentHistory(data varchar2(80));
Write a Program to enter details into Student table but not in Sunday
CREATE OR REPLACE TRIGGER holiday
BEFORE INSERT OR DELETE OR UPDATE ON Student
BEGIN
IF RTRIM(TO_CHAR(SYSDATE,'Day'))='Sunday' THEN
RAISE_APPLICATION_ERROR(-20002,'Today Holiday...');
END IF;
END;
/
SQL> insert into student values (10,'praveena','MTech');
4) PL/SQL Function
The PL/SQL Function is very similar to PL/SQL Procedure. The main difference between
procedure and a function is, a function must always return a value, and on the other hand a
procedure may or may not return a value. Except this, all the other things of PL/SQL
procedure are true for PL/SQL function to.
1. CREATE [OR REPLACE] FUNCTION function_name [parameters]
2. [(parameter_name [IN | OUT | IN OUT] type [, ...])]
3. RETURN return_datatype
4. {IS | AS}
5. BEGIN
6. < function_body >
7. END [function_name];
Here:
5
o IN represents that value will be passed from outside and OUT represents that this
parameter will be used to return a value outside of the procedure.
BEGIN
r:=n*n;
return r;
END;
/
PL/SQL Program to be executed on above function
SET SERVEROUTPUT ON
SET VERIFY OFF
DECLARE
n number(5) := &n;
r number(5);
BEGIN
r:=sqr(n);
DBMS_OUTPUT.PUT_LINE('Square of '||n||' is '||r);
END;
/
6
5) Exceptions:
What is Exception
PL/SQL facilitates programmers to catch such conditions using exception block in the program and an
appropriate action is taken against the error condition.
o System-defined Exceptions
o User-defined Exceptions
1. DECLARE
2. <declarations section>
3. BEGIN
4. <executable command(s)>
5. EXCEPTION
6. <exception handling goes here >
7. WHEN exception1 THEN
8. exception1-handling-statements
9. WHEN exception2 THEN
10. exception2-handling-statements
11. WHEN exception3 THEN
12. exception3-handling-statements
7
13. ........
14. WHEN others THEN
15. exception3-handling-statements
16. END;
Write a PL/SQL Program on Exception Handling
SET SERVEROUTPUT ON
SET VERIFY OFF
DECLARE
emp_rec emp%ROWTYPE;
BEGIN
SELECT * INTO emp_rec FROM Emp WHERE empno=&eno;
DBMS_OUTPUT.PUT_LINE('Name : '||emp_rec.ename);
DBMS_OUTPUT.PUT_LINE('Job : '||emp_rec.job);
DBMS_OUTPUT.PUT_LINE('Salary : '||emp_rec.sal);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Not Such Record Found.....');
DBMS_OUTPUT.PUT_LINE('SQL Error : '||sqlerrm);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('SQL Error : '||sqlerrm);
END;
/
Output:
Enter value for eno: 7934
Name : MILLER
Job : CLERK
Salary : 1300