PLSQL

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

set serveroutput on

DECLARE
v1 integer:=100;
v2 varchar(5):='A';
BEGIN
DECLARE
v1 integer;
v3 varchar2(10):=v2||'1';
v4 varchar2(10):='B';
BEGIN
dbms_output.put_line('v1(1)='||v1||chr(10)||
'v2(1)='||v2||chr(10)||'v3(1)='||v3);
END;
dbms_output.put_line('v1(2)='||v1||chr(10)||'v2(2)='||v2);
END;
/

**clause select
DECLARE
v_emp employees%ROWTYPE;
BEGIN
Select * into v_emp From employees where employee_id = 124;
dbms_output.put_line(’Nom employe :’ || v_emp.last_name || chr(10)||
’Fonction : ’ || v_emp.job_id || chr(10)|| ’Departement : ’||
v emp.department_id ||chr(10)|| ’Date recrutement : ’||
to char(v_emp.hire_date, ’dd/mm/yyyy’) || chr(10) || ’Salaire : ’ ||
v_emp.salary );
END;
/

**loop
DECLARE
v1 integer :=1;
BEGIN
FOR v1 IN 1..10 LOOP
dbms_output.put_line(v1);
END LOOP;
END;
/
DECLARE
v1 integer :=1;
BEGIN
LOOP
dbms_output.put_line(v1);
EXIT WHEN v1 =10;
v1 := v1+1;
END LOOP;
END;
/

**structure case
DECLARE
v1 integer := 18;
BEGIN
CASE
WHEN v1

<5 THEN
dbms_output.put_line(’A’);
WHEN v1

<10 THEN
dbms_output.put_line(’B’);
WHEN v1

<15 THEN
dbms_output.put_line(’C’);
ELSE dbms_output.put_line(’X’);
END CASE;
END;
/

**if/elsif/else
DECLARE
v1 number:= 685;
v2 number := 125;
v3 number :=870;
BEGIN
IF v1 < v2 THEN
IF v2<v3 then
dbms_output.put_line(’v1 < v2 < v3’);
ELSIF v3 < v1 then
dbms_output.put_line(’v3 < v1 < v2’);
ELSE
dbms_output.put_line(’v1 <=v3 <v2’);
END IF;
ELSIF v1 < v3 then
dbms_output.put_line(’v2 < v1 < v3’);
ELSIF v3<v2 then
dbms_output.put_line(’v3 < v2 <= v1’);
ELSE
dbms_output.put_line(’v2 <= v3 <= v1’);
END IF;
END;
/

**cursor version1:
DECLARE
CURSOR cur emp IS
SELECT employee_id, first_name, last_name
FROM employees
WHERE department_id = 100;
v_no_employees.employee_id%TYPE;
v_Fname_employees.first_name%TYPE;
v Lname_employees.last_name%TYPE;
BEGIN
IF NOT cur_emp%ISOPEN THEN
OPEN cur_emp;
END IF;
FETCH cur_emp INTO v_no, v_Fname,
v_Lname;
WHILE cur_emp%FOUND
LOOP
DBMS_OUTPUT.PUT_LINE(’Employe no:
’||v_no||’ Nom: ’||v_Fname||’Prenom: ’||v_Lname);
FETCH cur_emp INTO v_no, v_Fname,
v_Lname;
END LOOP;
END;

**cursor version 2
DECLARE
CURSOR cur_emp IS
SELECT employee_id, first_name, last_name
FROM employees
WHERE department_id = 100;
v_no employees.employee_id%TYPE;
v_Fname employees.first_name%TYPE;
v_Lname employees.last_name%TYPE;
BEGIN
IF NOT cur_emp%ISOPEN THEN
OPEN cur_emp;
END IF;
LOOP
FETCH cur_emp INTO v_no, v_Fname,
v_Lname;
EXIT WHEN cur_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(’Employe no:
’||v_no||’ Nom: ’||v_Fname||’Prenom: ’||v_Lname);
END LOOP;
close cur_emp
END;

set serveroutput on
DECLARE
CURSOR cur_emp IS SELECT employee_id, last_name FROM employees;
v_no employees.employee_id%TYPE;
v_Lname employees.last_name%TYPE;
BEGIN
IF NOT cur_emp%ISOPEN THEN
OPEN cur_emp;
END IF;
FETCH cur_emp INTO v_no, v_Lname;
WHILE cur_emp%FOUND
LOOP
dbms_output.put_line(cur_emp%rowcount);
FETCH cur_emp INTO v_no, v_Lname;
END LOOP;
CLOSE cur_emp;
END;

set serveroutput on
DECLARE
CURSOR cur_emp IS
SELECT employee_id FROM employees WHERE department_id = 100;
v_no employees.employee_id%TYPE;
BEGIN
OPEN cur_emp;
LOOP
dbms_output.put_line(cur_emp%rowcount);
FETCH cur_emp INTO v_no;
EXIT WHEN cur_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(’Employe no: ’||v_no);
END LOOP;
close cur_emp;
END;
/

**cursor/record
DECLARE
CURSOR cur_emp IS SELECT employee_id, last_name FROM employees;
rec_emp cur_emp%ROWTYPE;
BEGIN
OPEN cur_emp;
LOOP
FETCH cur_emp INTO re_emp;
EXIT WHEN cur_emp%NOTFOUND;
dbms_output.put_line(’Employ ́e no: ’||rec_emp.employee_id||’ Nom:
’||rec_emp.last_name);
END LOOP;
CLOSE cur_emp;
END;

**for in cursor
DECLARE
CURSOR cur_emp IS SELECT employee_id, last_name FROM employees;
BEGIN
FOR rec_emp IN cur_emp
LOOP
dbms_output.put_line(’Employ ́e no: ’||rec_emp.employee_id||’ Nom:
’||rec_emp.last_name);
END LOOP;
END;

*curseur paramétré
DECLARE
CURSOR cur_emp(v_dept number,v_sal employees.salary%TYPE) IS SELECT
employee_id, last_name,salary FROM employees where department_id=v_dept and
salary>v_sal;
rec_emp cur_emp%ROWTYPE;
BEGIN
OPEN cur_emp(80,6000);
LOOP
FETCH cur_emp INTO rec_emp;
EXIT WHEN cur_emp%NOTFOUND;
dbms_output.put_line(’Employe no: ’||rec_emp.employee_id||’ Nom:
’||rec_emp.last_name||’ Salaire: ’||rec_emp.salary);
END LOOP;
CLOSE cur_emp;
END;

--------------
mode de passage : in/out/in out
--------------
**procédure stocké
CREATE OR REPLACE PROCEDURE add_dept (dept id IN
departments.department_id%TYPE, dept_name IN
departments.department_name%TYPE, nbre OUT number)
IS
BEGIN
insert into departments(department_id,department_name)
values(dept_id,dept_name);
commit;
select count(*) into nbre from departments;
dbms_output.put_line(’Le nombre de d ́epartement est : ’||nbre);
END;

**appel de procédure stockés


DECLARE
nb number;
BEGIN
add_dept(300,’IT’,nb);
end;

**fonction stockés
CREATE OR REPLACE FUNCTION fn check_sal (empno employees.employee_id%TYPE)
RETURN Boolean IS
dept_id employees.department_id%TYPE;
sal_employees.salary%TYPE;
avg_sal employees.salary%TYPE;
BEGIN
SELECT salary,department_id INTO sal,dept_id FROM employees WHERE
employee_id=empno;
SELECT avg(salary) INTO avg_sal FROM employees WHERE department_id=dept_id;
IF sal > avg_sal THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END;

**appel fonction stockés


BEGIN
IF (fn_check_sal(124)) THEN
DBMS_OUTPUT.PUT_LINE(’Salary > average’);
ELSE
DBMS_OUTPUT.PUT_LINE(’Salary < average’);
END IF;
END;

-------------------------------------------------- cursor
declare
cursor c_tab is select table_name from user_tables;
cursor c_col(P_tab user_tables.table_name%type)
is select column_name from usr_tab_columns where table_name=P_tab;
res varchar2(2000);
x number;
begin
for vc_tab in c_tab loop
res:=vc_tab.table_name||'(';
for vc_col in c_col(vc_tab.table_name) loop
if c_col%rowcount=1 then
res:=res||vc_col.column_name;
end if;
end loop;
res:=res||')';
excute immdiate 'selct count(*) from '||vc_tab.table_name into x;
dbms_output.put_line(res||'['||x||']');
end loop;
end;
/
declare
cursor c_tab is select table_name from user_tables;
cursor c_col(P_tab user_tables.table_name%type) is select column_name
from user_tab_columns where table_name =P_tab;
res varchar2(2000);
begin
for vc_tab in c_tab loop
res:= vc_tab.table_name||'(';
for vc_col in c_col(vc_table.table_name) loop
if c_col%rowcount = 1 then
res:= res || vc_col.column_name;
end if;
end loop;
res:= res ||')';
dbms_output.put_line(res);
end loop;
end;
/

You might also like