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

PLSQL Notes

The document contains examples of PL/SQL code using various programming constructs like variables, conditional statements, loops, case statements, and handling null values. It shows how to declare variables, perform calculations, return output, and iterate through loops. Multiple examples are provided to demonstrate different programming techniques in PL/SQL.

Uploaded by

47Rutuja Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

PLSQL Notes

The document contains examples of PL/SQL code using various programming constructs like variables, conditional statements, loops, case statements, and handling null values. It shows how to declare variables, perform calculations, return output, and iterate through loops. Multiple examples are provided to demonstrate different programming techniques in PL/SQL.

Uploaded by

47Rutuja Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

---------- 10/08/2023 -------------------------

set verify on;

set verify off;

set serveroutput on;

DECLARE
V_SUB1 NUMBER(4) := &NUM1;
--V_SUB2 NUMBER(4) := &NUM2;
V_SUB2 V_SUB1%TYPE := &NUM2;
V_SUB3 NUMBER(4) := &NUM3;
V_PERC NUMBER(4);
BEGIN
V_PERC := (V_SUB1 + V_SUB2 + V_SUB3)*100/300;
IF V_PERC >= 75 THEN
DBMS_OUTPUT.PUT_LINE('PASSING WITH DISTINCTION');
ELSIF V_PERC BETWEEN 75 AND 61 THEN
DBMS_OUTPUT.PUT_LINE('PASSING WITH FIRST CLASS');
ELSIF V_PERC BETWEEN 60 AND 51 THEN
DBMS_OUTPUT.PUT_LINE('PASSING WITH SECOND CLASS');
ELSIF V_PERC BETWEEN 50 AND 35 THEN
DBMS_OUTPUT.PUT_LINE('JUST PASSED');
ELSE
DBMS_OUTPUT.PUT_LINE('FAILURE');
END IF;
END;

Declare
v_eng number(4) := 50;
v_maths number(4) := 55;
v_history number(4) := 43;
v_science number(4) := 69;
grade number(10);

BEGIN
grade := (v_eng + v_maths + v_history + v_science)/400*100;

if grade >70 then


dbms_output.put_line('Disction Class');
elsif grade between 61 and 70 then
dbms_output.put_line('First Class');
elsif grade between 51 and 60 then
dbms_output.put_line('Second Class');
elsif grade between 35 and 50 then
dbms_output.put_line('Just Pass');
else
dbms_output.put_line('Failed');
end if;
end;

declare
marks1 number(5):=50;
marks2 number(5):=60;
marks3 number(5):=20;
marks4 number(5):=60;
total_marks NUMBER(5);
percentage NUMBER(5);
BEGIN
total_marks := marks1 + marks2 + marks3 + marks4;
percentage := total_marks*100/400;
DBMS_OUTPUT.PUT_LINE(percentage);

IF percentage<=35 THEN
DBMS_OUTPUT.PUT_LINE('failed');
ELSIF percentage BETWEEN 35 AND 50 THEN
DBMS_OUTPUT.PUT_LINE('pass');
ELSIF percentage BETWEEN 51 AND 60 THEN
DBMS_OUTPUT.PUT_LINE('SECOND CLASS');
ELSIF percentage BETWEEN 61 AND 70 THEN
DBMS_OUTPUT.PUT_LINE('FIRST CLASS');
ELSE
DBMS_OUTPUT.PUT_LINE('disctinction');
END IF;
END;

DECLARE
V_ENG NUMBER :=&NUM;
V_MATH NUMBER :=&NUM;
V_EVS NUMBER :=&NUM;
V_SCI NUMBER :=&NUM;
PERCEN NUMBER;
BEGIN
PERCEN:=(V_ENG+V_MATH+V_EVS+V_SCI)/400*100;
IF PERCEN>=70 THEN
DBMS_OUTPUT.PUT_LINE(PERCEN|| 'Disctinction');
ELSIF PERCEN>=61 THEN
DBMS_OUTPUT.PUT_LINE(PERCEN|| 'FIRST CLASS');
ELSIF PERCEN>=51 THEN
DBMS_OUTPUT.PUT_LINE(PERCEN|| 'SECOND CLASS');
ELSIF PERCEN>=35 THEN
DBMS_OUTPUT.PUT_LINE(PERCEN|| 'PASS');
ELSIF PERCEN<35 THEN
DBMS_OUTPUT.PUT_LINE(PERCEN|| 'FAIL');
END IF;
END;

--- write a plsql prog. to display salary grade of employee


take emp id from user and display employee's sal and grade

grade good sal is 1 to 3999


Average sal is 4000 to 7999
Very Good sal is 8000 to 10999
Excellent sal is >11000

DECLARE
sal employees.salary%TYPE;
BEGIN
SELECT
salary
INTO sal
FROM
employees
WHERE
employee_id = &employee_id;

IF
sal >= 1
AND sal <= 3999
THEN
dbms_output.put_line(sal || 'Good Salary');
ELSIF
sal >= 4000
AND sal <= 7999
THEN
dbms_output.put_line(SAL || 'Average Salary');
ELSIF
sal >= 8000
AND sal <= 10999
THEN
dbms_output.put_line(sal || 'Very Good Salary');
ELSIF sal >= 11000 THEN
dbms_output.put_line(sal || 'Excellent');
END IF;

END;
-----

declare
v_sal employees.salary%type;
v_empid employees.employee_id%type;
begin
select employee_id, salary into v_empid, v_sal from employees
where employee_id=&emp_id;
if v_sal>11000 then
dbms_output.put_line('salary of employee ' || v_empid || ' is ' || v_sal || ' and
grade is Excellent');
elsif v_sal>=10999 then
dbms_output.put_line('salary of employee ' || v_empid || ' is ' || v_sal || ' and
grade is very good');
elsif v_sal>=7999 then
dbms_output.put_line('salary of employee ' || v_empid || ' is ' || v_sal || ' and
grade is Average sal');
else
dbms_output.put_line('salary of employee ' || v_empid || ' is ' || v_sal || ' and
grade is good');
end if;
end;

-----

DECLARE
sal employees.salary%TYPE;
V_EMP EMPLOYEES.EMPLOYEE_ID%TYPE;
BEGIN
SELECT salary,employee_id
INTO sal,V_EMP
FROM
employees
WHERE
employee_id = &employee_id;

IF
sal >= 1
AND sal <= 3999
THEN
dbms_output.put_line('Employee number ' ||v_emp|| ' has salary: '|| sal );
dbms_output.put_line(sal || ' Good Salary');
ELSIF
sal >= 4000
AND sal <= 7999
THEN
dbms_output.put_line('Employee number ' || v_emp|| ' has salary: '||
sal );
dbms_output.put_line(SAL || ' Average Salary');
ELSIF
sal >= 8000
AND sal <= 10999
THEN
dbms_output.put_line('Employee number ' || v_emp|| ' has salary: '||
sal );
dbms_output.put_line(sal || ' Very Good Salary');
ELSIF sal >= 11000 THEN
dbms_output.put_line('Employee number ' || v_emp|| ' has salary: '||
sal );
dbms_output.put_line(sal || ' Excellent');
END IF;
END;

-- CASE Expression ---

-- it selects the result and returns it and to select result we use expressions.

declare
v_grade char(1):= upper('&g');
--v_grade char(1):= '&g';
v_appraisal varchar(20);

begin
v_appraisal := CASE v_grade
when 'A' then 'Good'
when 'B' then 'Very Good'
when 'C' then 'Excellent'
ELSE 'Not bad'
End;

dbms_output.put_line('Grade is: '||v_grade);


dbms_output.put_line('Appraisal is: '||v_appraisal);

end;

-- Searched CASE Expression


declare
v_sal employees.salary%type;
V_salgrade varchar2(20);

begin
select salary into v_sal from employees where employee_id=&eid;

V_salgrade := case
when v_sal>11000 then 'Excellent'
when v_sal>8000 then 'Very Good'
when v_sal>4000 then 'Good'
else 'Not Bad'
end;

dbms_output.put_line('Your Salary is: '||v_sal);


dbms_output.put_line('Your Salary Grade is: '||V_salgrade);
end;

---- Handling Null

-- Simple comparison involving null yields NULL


-- applying logical operator NOT to a null value yields NULL
-- if your condition yields null then it's subsequent statement won't run

--- TRUE AND TRUE -- TRUE


--- TRUE AND FALSE -- FALSE
-- NULL AND TRUE -- NULL
-- NULL AND FALSE -- FALSE

declare
v_sal employees.salary%type;
v_empid employees.employee_id%type;
v_sal_1 employees.salary%type :=8000;
v_job varchar(5):='Prog';
begin
--select employee_id, salary into v_empid, v_sal from employees
--where employee_id=&emp_id;
if v_sal>11000 and v_job='Man' then
dbms_output.put_line('salary of employee ' || v_empid || ' is ' || v_sal || ' and
grade is Excellent');
elsif v_sal>=10999 and v_job='Prog' then
dbms_output.put_line('salary of employee ' || v_empid || ' is ' || v_sal || ' and
grade is very good');
elsif v_sal_1>=7999 then
dbms_output.put_line('salary of employee ' || v_empid || ' is ' || v_sal || ' and
grade is Average sal');
else
dbms_output.put_line('salary of employee ' || v_empid || ' is ' || v_sal || ' and
grade is good');
end if;
end;

-- Looping statement
-- loop repeats statement multiple times.

-- Types of loop
-- Basic Loop
-- While Loop
-- For Loop

-- Basic Loop

-- Syntax:

LOOP
statements;
...
EXIT [WHEN condition];
END LOOP;

declare
v_num number(2):=10;

begin
loop
v_num:=v_num+1;
dbms_output.put_line(v_num);
EXIT WHEN v_num>9;
END LOOP;
end;

declare
v_num number(2):=10;

begin
loop
EXIT WHEN v_num>9;
v_num:=v_num+1;
dbms_output.put_line(v_num);

END LOOP;
end;

declare
v_num number(2):=11;

begin
loop
v_num:=v_num-1;
dbms_output.put_line(v_num);
EXIT WHEN v_num<0;
END LOOP;
end;

--- WHILE Loop

/*
WHILE condition loop
statements..
....
end loop;

*/

declare
v_num number(2):=0;

begin
while v_num<10 loop
v_num:=v_num+1;
dbms_output.put_line(v_num);
END LOOP;
end;

declare
v_num number(2):=10;

begin
while v_num>=1 loop
dbms_output.put_line(v_num);
v_num:=v_num-1;
END LOOP;
end;

--- FOR loop

-- do not declare counter variable. it id declared implicity


-- we can use counter variable as target of any assignment statement
/*

FOR counter IN [REVERSE] lower_bound..upper_bound


loop

end loop;

*/

declare

begin
FOR n IN REVERSE 1..10
loop
dbms_output.put_line(n);
end loop;
-- dbms_output.put_line(n);

end;
declare
--v_num number(3):=2;

begin

FOR n IN 1..10
loop
if mod(n,2)!=0 then
dbms_output.put_line(n);
end if;
end loop;

-- dbms_output.put_line(n);
end;

You might also like