Вы находитесь на странице: 1из 21

Chapter 8 PL/SQL

What is PL/SQL?

PL/SQL is Oracles procedural extension to SQL. PL/SQL is not a separate product; it is a technology integrated into the Oracle server.

PL/SQL Block Structure


PL/SQL is a block structure programming language. A PL/SQL block has the following structure: [DECLARE]

--variable declaration

BEGIN
--executable section

[EXCEPTION]
--error handling routines

END;

Variables and Constants


Variables/Constants are declared with the following syntax: Var_name [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expression] E.g. X Number := 200; Tot_salary Number(10,2) := 0; Tot_salary Number(10,2) DEFAULT 0; Zero_value constant number := 0;

Named and Anonymous Blocks

A PL/SQL block can be


Anonymous Named

Functions

Takes zero or more parameter values and returns one value. Takes zero or more parameter values ; returns values only through parameters.

Procedure

PL/SQL Anonymous Block


Declare v_comm_percent constant number := 10; Begin update emp set comm = sal * v_comm_percent where deptno = 10; End; /

Procedure

To make the previous example, a stored procedure.

Create or replace procedure update_comm (v_dept in number, v_percent in number default 10) Is begin update emp set comm = sal * v_percent where deptno = v_dept; end; / SQL> execute upadate_comm (10,15);

Function

A function is executed like any other SQL built-in function

Create or replace function calc_area (v_len in number, v_width in number) return number Is v_area number Begin v_area := v_len * v_width; return v_area; End; / SQL> select calculate_area (10,14) from dual;

Control Structures

IF .. THEN.. END IF IF .. THEN.. ELSEEND IF IF .. THEN..ELSIF .. END IF

If sal< 1000 then bonus := 200; Elsif sal < 3000 then bonus := 300; Elsif sal < 5000 then bonus := 400; Elsif sal < 7000 then bonus := 500; Else bonus := 0; End if;

Loop
X:= 100; LOOP X := X+10; if x > 1000 then exit; end if; End loop; Y := x;

X:=100 Loop exit when x > 1000; x= x+10; End loop; Y:=x;

While Loop
x := 100; While x <= 1000 loop x:=x+10; End loop; Y:=x;

For .. Loop
X:=100; For ctr in 1..10 loop
X:=x+10;

End loop; Y:=x;

Accessing the database

SQL is the language used to access the database. DML commands are supported in PL/SQL. DDL statements are not supported in PL/SQL.

Queries

When using the SELECT statement in PL/SQL, always make sure that the INTO clause is present . The values returned from the query are assigned to the variables in the INTO clause. The variables should be defined earlier in the declaration section and must be compatible with the datatypes of the column values selected.

An Example
PL/SQL code to list the highest paid employee. SQL> Set serveroutput on SQL> Declare v_empno number(4); v_ename varchar2(10); v_sal number(7,2);

Begin select empno,ename,sal into v_empno,v_ename,v_sal from emp where sal = (select max(sal) from emp);

dbms_output.put_line(Highest paid employee is || v_ename); dbms_output.put_line(ID is || v_empno || Salary is || v_sal);


End;

The %Type Attribute


To avoid type and size conflict between a variable and the column of a table the %TYPE attribute is used. E.g. V_name emp.ename%type; V_name scott.emp.ename%type; V_sal emp.sal%type; Advantage of this method of defining a variable Whenever the type and/or size of a column in the table is changed, it is automatically reflected in the variable declaration.

The %ROWTYPE Attribute

The %ROWTYPE attribute provides a record type that represents a row in a table. E.g. emp_row_var emp%rowtype; To access individual members

Emp_row_var.sal := 5000;

Example

Write a PL/SQL code to update the salary of employee number 7788 to 3000 if salary is less than 3000.
declare v_sal number(7,2); begin select sal into v_sal from emp where empno =7788; if v_sal< 3000 then update emp set sal = 3000 where empno = 7788; end if; end;

Example

Write a PL/SQL code to insert all details of employee no 7698 to a new table temp which has the same structure as table emp. declare newrec emp%rowtype; begin select * into newrec from emp where empno = 7698; insert into temp values (newrec.empno, newrec.ename, newrec.job, newrec.mgr, newrec.hiredate, newrec.sal, newrec.comm, newrec.deptno);

end;

Example

Write a PL/SQL code to update the commission of employee number 7369 to 300 if it is NULL, otherwise raise his commission by 25%.

Declare var_empno number(4) := 7369; var_comm emp.comm%TYPE; Begin select comm into var_comm from emp where empno = var_empno;
if var_comm IS NULL then update emp set comm = 300 where empno = var_empno; else var_comm := var_comm + var_comm * 0.25; update emp set comm = var_comm where empno = var_empno; end if; End;

END..

Вам также может понравиться