12.07.2015 Views

Oracle SQL Developer

Oracle SQL Developer

Oracle SQL Developer

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Utilizing the Main Features of PL/<strong>SQL</strong>inside a subprogram. Any update to the value of the parameter inside the subprogramis not accessible outside the subprogram.Example 7–21Declaring a Function With IN ParametersDECLARE -- declare variables and subprogramsfname VARCHAR2(20) := 'randall';lname VARCHAR2(25) := 'dexter';FUNCTION upper_name ( v1 IN VARCHAR2, v2 IN VARCHAR2)RETURN VARCHAR2 ASv3 VARCHAR2(45); -- this variable is local to the functionBEGIN-- build a string that will be returned as the function valuev3 := v1 || ' + ' || v2 || ' = ' || UPPER(v1) || ' ' || UPPER(v2);RETURN v3; -- return the value of v3END;BEGIN-- call the function and display resultsDBMS_OUTPUT.PUT_LINE(upper_name (fname, lname));END;/In Example 7–22, both a variable and a numeric literal are passed as a parameter to amore complex procedure.Example 7–22 Declaring a Complex Procedure in a PL/<strong>SQL</strong> BlockDECLARE -- declare variables and subprogramsempid NUMBER;PROCEDURE avg_min_max_sal (empid IN NUMBER) ISjobid VARCHAR2(10);avg_sal NUMBER;min_sal NUMBER;max_sal NUMBER;BEGIN-- determine the job Id for the employeeSELECT job_id INTO jobid FROM employees WHERE employee_id = empid;-- calculate the average, minimum, and maximum salaries for that job IdSELECT AVG(salary), MIN(salary), MAX(salary) INTO avg_sal, min_sal, max_salFROM employees WHERE job_id = jobid;-- display dataDBMS_OUTPUT.PUT_LINE ('Employee Id: ' || empid || ' Job Id: ' || jobid);DBMS_OUTPUT.PUT_LINE ('The average salary for job Id: ' || jobid|| ' is ' || TO_CHAR(avg_sal));DBMS_OUTPUT.PUT_LINE ('The minimum salary for job Id: ' || jobid|| ' is ' || TO_CHAR(min_sal));DBMS_OUTPUT.PUT_LINE ('The maximum salary for job Id: ' || jobid|| ' is ' || TO_CHAR(max_sal));END avg_min_max_sal;BEGIN-- call the procedure with several employee Idsempid := 125;avg_min_max_sal(empid);avg_min_max_sal(112);END;/Subprograms can also be declared in packages. You can create subprograms that arestored in the database. These subprograms can be called from other subprograms,packages, and <strong>SQL</strong> statements. See Subprograms and Packages: Usage Information.7-16 <strong>SQL</strong> <strong>Developer</strong> Online Help

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!