Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DBMS PLSQL

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

1)Conditional Statements in PL/SQL

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.

1) IF condition THEN statement1;


ELSE
statement 2;
END IF;
2) IF condition 1 THEN statement 1; statement2;
ELSIF condtion2 THEN
statement 3; ELSE
statement 4; END

Write a PL/SQL program to find the Arithimetic Operators by using if,else if,else

SET SERVEROUTPUT ON

SET VERIFY OFF

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);

ELSIF ch=3 THEN

r:=a*b;

DBMS_OUTPUT.PUT_LINE('Multiplication : '||r);

ELSIF ch=4 THEN

r:=a/b;

DBMS_OUTPUT.PUT_LINE('Division : '||r);

ELSIF ch=5 THEN

r:=mod(a,b);

DBMS_OUTPUT.PUT_LINE('Remainder : '||r);

ELSE

DBMS_OUTPUT.PUT_LINE('Invalid Choice... ');

END IF;

END;

Output:

Enter value for a: 10

Enter value for b: 8

Enter value for ch: 1

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

Write a PL/SQL program on for Loop

SET SERVEROUTPUT ON

SET VERIFY OFF

DECLARE

n number(3):=&n;

BEGIN

FOR i IN 1..n

LOOP

DBMS_OUTPUT.PUT_LINE(i);

END LOOP;

END;

Output:

Enter value for n: 3

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 are written to be executed in response to any of the following events.

o A database manipulation (DML) statement (DELETE, INSERT, or UPDATE).


o A database definition (DDL) statement (CREATE, ALTER, or DROP).
o A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).

Triggers could be defined on the table, view, schema, or database with which the event is associated.

Syntax for creating trigger:

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.

Syntax to create a function:

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:

o Function_name: specifies the name of the function.


o [OR REPLACE] option allows modifying an existing function.
o The optional parameter list contains name, mode and types of the parameters.

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.

Write a PL/SQL Program to Create a Function

CREATE OR REPLACE FUNCTION sqr(n number) RETURN number IS


r number;

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

An error occurs during the program execution is called Exception in PL/SQL.

PL/SQL facilitates programmers to catch such conditions using exception block in the program and an
appropriate action is taken against the error condition.

There are two type of exceptions:

o System-defined Exceptions
o User-defined Exceptions

PL/SQL Exception Handling


Syntax for exception handling:

Following is a general syntax for exception handling:

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

You might also like