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

Proceduri Stocate

The document discusses stored procedures in Oracle including examples of creating and calling stored procedures. It provides 4 exercises: 1) a procedure that modifies an employee's salary based on input parameters, 2) a procedure that returns a cursor, 3) a procedure that uses MERGE to insert or update an employee record, and 4) procedures that take IN, OUT and IN OUT parameters to calculate taxes.

Uploaded by

IoanaMiruna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Proceduri Stocate

The document discusses stored procedures in Oracle including examples of creating and calling stored procedures. It provides 4 exercises: 1) a procedure that modifies an employee's salary based on input parameters, 2) a procedure that returns a cursor, 3) a procedure that uses MERGE to insert or update an employee record, and 4) procedures that take IN, OUT and IN OUT parameters to calculate taxes.

Uploaded by

IoanaMiruna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

PROCEDURI STOCATE

E-learning – proceduri stocate

https://www.techonthenet.com/oracle/procedures.php

https://www.mkyong.com/oracle/oracle-stored-procedures-hello-world-examples/

http://psoug.org/reference/procedures.html

https://www.mkyong.com/oracle/oracle-stored-procedure-cursor-example/

https://docs.oracle.com/cd/B28359_01/appdev.111/b28844/procedures_plsql.htm#TDPNG60000

EXERCITIU 1: primeste ca param id angajat si creste salariul ang resp cu un al doilea param:

create or replace procedure ModificaSalAngajat(

pEmpID employees.employee_id%type,

pValModificare int

IS

BEGIN

DBMS_OUTPUT.PUT_LINE('val parametri=' || pEmpID || ' pValModificare=' ||


to_char(pValModificare));

update employees set salary = salary + pValModificare

where employee_id = pEmpID;

DBMS_OUTPUT.PUT_LINE(sql%rowcount);

END;
*aici ca sa testez procedura

EXERCITIU 2: VA APELA O PROCEDURA SI VA RETURNA UN CURSOR. Se poate apela si din alte lb de


progr:

create or replace procedure Angajati_FCTDE_Departament(

pDepId departments.department_id%type,

cListaAng out SYS_REFCURSOR


)

IS

BEGIN

open cListaAng FOR

SELECT * FROM EMPLOYEES WHERE DEPARTMENT_ID=pDepId;

END;

Apelam asa:

set SERVEROUTPUT ON;

declare

cout SYS_REFCURSOR;

rand employees%rowtype;

begin

Angajati_FCTDE_Departament(30, cout);

loop

fetch cout into rand;


exit when cout%notfound;

DBMS_OUTPUT.PUT_LINE (rand.last_name || ' ' || rand.salary);

end loop;

end;

Aici nu merge for pt ca rand tb declarat?

SYS_REFCURSOS  pt a emite date catre lb de programare(java etc)

EXERCITIU 3: PROCEDURA PRIN CARE SA ADAUGAM IN BD IN TABELA ANGAJAT. DACA ANG SE FACE
UPDATE LA SAL SAU LA DEP, DACA NU EXISTA SE ADAUGA. CU DML MERGE

https://www.oracletutorial.com/oracle-basics/oracle-merge/

https://livesql.oracle.com/apex/livesql/file/content_PC76LRDFFAVI6P6LMCWJLBOP2.html

declare

pEmpId ANGAJAT.employee_id%type := 100;

pnume ANGAJAT.last_name%type := 'dora';

pprenume ANGAJAT.first_name%type := 'dora';

psal ANGAJAT.salary%type := 4500;

pdepid ANGAJAT.department_id%type := 10;

begin

merge into ANGAJAT a


using(SELECT pEmpId as EmpId,

pnume as nume,

pprenume as prenume,

psal as sal,

pdepid as depid from DUAL ) p

on (a.Employee_id = p.EmpId)

WHEN MATCHED THEN

UPDATE SET a.salary=p.sal

-- WHERE a.salary > 5000

-- AND (a.salary <> p.sal)

WHEN NOT MATCHED THEN

INSERT(a.employee_id, a.last_name, a.first_name, a.salary, a.department_id)

VALUES (p.EmpId, p.nume, p.prenume, p.sal, p.depid);

DBMS_OUTPUT.put_line(sql%rowcount);

commit; --!!!

end;

testez – asa ar tb sa dea

Ex 4:
Intai adaug un ang cu sal=30000

Ex: creare procedura PARAMETRU IN

create or replace PROCEDURE calc_tax (

empl IN employees.employee_id%TYPE,

t1 IN NUMBER

) IS

tax NUMBER;

sal NUMBER;

BEGIN

IF t1 < 0 OR t1 > 60 THEN

raise_application_error(-20000, 'procent incorect');

END IF;

SELECT

salary

INTO sal
FROM

employees

WHERE

employee_id = empl;

tax := sal * t1 / 100;

dbms_output.put_line('SALARY: ' || sal);

dbms_output.put_line('TAX: ' || tax);

EXCEPTION

WHEN no_data_found THEN

dbms_output.put_line('nu exista angajatul');

END calc_tax;

SET SERVEROUTPUT ON

BEGIN

calc_tax(100, 20);

END;

Ex: creare procedura parametru OUT

create or replace PROCEDURE calc_tax_OUT (

empl IN employees.employee_id%TYPE,

t1 IN NUMBER,

r1 OUT NUMBER
) IS

-- tax NUMBER;

sal NUMBER;

BEGIN

IF t1 < 0 OR t1 > 60 THEN

raise_application_error(-20000, 'procent incorect');

END IF;

SELECT

salary

INTO sal

FROM

employees

WHERE

employee_id = empl;

R1 := sal * t1 / 100;

dbms_output.put_line('SALARY: ' || sal);

EXCEPTION

WHEN no_data_found THEN

dbms_output.put_line('nu exista angajatul');

END calc_tax_out;

SET SERVEROUTPUT ON

DECLARE

r NUMBER;

BEGIN

r := 0;

calc_tax_out(100, 20, r);


dbms_output.put_line('r= ' || r);

END;

1- Crear un procedimiento llamado visualizar que visualice el nombre y salario de todos los
empleados.

2- Modificar el programa anterior para incluir un parámetro que pase el número de


departamento para que visualice solo los empleados de ese departamento

Debe devolver el número de empleados en una variable de tipo OUT

CREATE OR REPLACE PROCEDURE VISUALIZAR(dep number, num out number) IS


CURSOR C1 IS SELECT FIRST_NAME, SALARY FROM EMPLOYEES
where department_id=dep;
NAME EMPLOYEES.FIRST_NAME%TYPE;
SALARY EMPLOYEES.SALARY%TYPE;
BEGIN
num:=0;
FOR I IN C1 LOOP
DBMS_OUTPUT.PUT_LINE(i.first_name || ' ' || i.salary);
num:=num+1;
END LOOP;
END VISUALIZAR;

set serveroutput on

declare

x number;

begin
visualizar(90,x);

DBMS_OUTPUT.put_line(x);

end;

You might also like