PLSQL Help
PLSQL Help
PLSQL Help
An Introduction
DECLARE
Declaration statements
BEGIN
Executable statements
EXCEPTION
Exception-handling statements
END ;
• The executable section contains executable statements that allow you to manipulate
the variables that have been declared in the declaration section.
BEGIN
SELECT first_name, last_name
INTO v_first_name, v_last_name
FROM student
WHERE student_id = 123 ;
DBMS_OUTPUT.PUT_LINE
(‘Student name :’ || v_first_name ||‘ ’|| v_last_name);
END;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE
(‘ There is no student with student id 123 ’)
END;
• Variables are
• Used to store numbers, character strings, dates, and other data values
• Avoid using keywords, table names and column names as variable names
• Must be declared with data type before use:
• variable_name data_type_declaration;
DECLARE
d dept%ROWTYPE;
BEGIN
SELECT deptno,dname,loc INTO d FROM dept WHERE deptno=10;
DBMS_OUTPUT.PUT_LINE(d.dname);
END;
• variable_name := value;
Assignment
Statements Value can be a literal/another variable:
• current_s_first_name := 'John';
• current_s_first_name := s_first_name;
Syntax
IF condition1 THEN
commands that execute if condition1 is TRUE;
ELSIF condition2 THEN
commands that execute if condition2 is TRUE;
ELSIF condition3 THEN
commands that execute if condition3 is TRUE;
...
ELSE
commands that execute if none of the
conditions are TRUE;
END IF;
CS 262: DBMS Lab
Types of IF Statemnets
• Simple IF statement:
Set the manager ID to 22 if the employee name is Osborne.
IF v_ename = 'OSBORNE' THEN v_mgr := 22;
END IF;
Set the job title to Salesman, the department number to 35, and the
commission to 20% of the current salary if the last name is Miller
IF v_ename = 'MILLER' THEN
v_job := 'SALESMAN';
v_deptno := 35;
v_new_comm := sal * 0.20;
END IF;
• IF-THEN-ELSE Statements
Set a flag for orders where there are fewer than five days between
order date and ship date.
IF v_shipdate - v_orderdate < 5 THEN
v_ship_flag := 'Acceptable';
ELSE
v_ship_flag := 'Unacceptable';
END IF;
• Nested IF Statement
For a given value, calculate a percentage of that value
based on a condition.
IF v_start > 100 THEN
v_start := 2 * v_start;
ELSIF v_start >= 50 THEN
v_start := .5 * v_start;
ELSE
v_start := .1 * v_start;
END IF;
Syntax
LOOP
statement1;
...
EXIT [WHEN condition];
END LOOP;
A basic loop can contain multiple EXIT statements.
DECLARE
v_ordid item.ordid%TYPE := 601;
v_counter NUMBER(2) := 1;
BEGIN
LOOP
INSERT INTO item(ordid, itemid)
VALUES(v_ordid, v_counter);
v_counter := v_counter + 1;
EXIT WHEN v_counter > 10;
END LOOP;
END;
DECLARE
v_ordid item.ordid%TYPE := 601;
BEGIN
FOR i IN 1..10 LOOP
INSERT INTO item(ordid, itemid)
VALUES(v_ordid, i);
END LOOP;
END;
Syntax
WHILE condition LOOP
statement1;
statement2;
...
END LOOP;
DECLARE
v_count NUMBER(2) := 1;
num_depts NUMBER := &num_depts ;
BEGIN
WHILE v_count <= num_depts LOOP
INSERT INTO dept(deptno,dname)
VALUES (v_count, &v_dept_name);
v_count := v_count + 1;
END LOOP;
COMMIT;
END;
• Even though the cursor stores multiple records, only one record
can be processed at a time, which is called as current row.
• When you fetch a row the current row position moves to next
row.
DECLARE
myempid number;
mysal number;
CURSOR emp_crsr IS
SELECT empid, salary FROM emp1;
BEGIN
OPEN emp_crsr;
LOOP
FETCH emp_crsr INTO myempid, mysal;
EXIT WHEN emp_crsr%NOTFOUND;
if myempid = 10 or myempid = 30 then
UPDATE emp1 SET salary = mysal + 5000 WHERE empid = myempid;
else
UPDATE emp1 SET salary = mysal + 1111 WHERE empid = myempid;
end if;
END LOOP;
END;
• CURSOR_ALREADY_OPEN
• INVALID_CURSOR
• NO_DATA_FOUND
• TOO_MANY_ROWS
• ZERO_DIVIDE
Declare
sal emp.salary%type;
BEGIN
select salary into sal from emp where eno = 1;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE(‘MORE THAN ONE ROW SELECTED…’ );
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(‘OTHER TYPE OF ERROR HAS OCCURED…’ );
END;
Declare
negative_salary EXCEPTION;
sal emp.salary%type;
BEGIN
select salary into sal from emp where eno = 1;
if (sal < 0) then
RAISE negative_salary;
else
DBMS_OUTPUT.PUT_LINE(‘Salary =‘||sal);
end if;
EXCEPTION
WHEN negative_salary THEN
DBMS_OUTPUT.PUT_LINE(‘INVALID SALARY…’ );
UPDATE EMP SET SALARY = 0 WHERE ENO = 1;
END;
CS 262: DBMS Lab
Functions &
Procedures
DECLARE
m number := &m;
n number;
BEGIN
FACT(m,n);
dbms_output.put_line(n);
END;
DECLARE
m number := &m;
n number;
BEGIN
n := FACT(m);
dbms_output.put_line(n);
END;
• Triggers are procedures that run implicitly when an INSERT, UPDATE, or DELETE
statement is issued against the associated table or, in some cases, against a view, or
when database system actions occur.
• A trigger has three basic parts:
• A triggering event or statement
• It is an SQL statement or event that causes a trigger to fire.
• A trigger restriction
• A trigger restriction specifies a Boolean expression that must be true for the
trigger to fire.
• A trigger action
• A trigger action is the procedure that contains the SQL statements and code to
be run when the trigger fires.