12.07.2015 Views

Oracle SQL Developer

Oracle SQL Developer

Oracle SQL Developer

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Managing Packagescalling procedures. Changes to a package spec require <strong>Oracle</strong> to recompile everystored subprogram that references the package.8.3.1 Calling Subprograms in PackagesPackages are stored in the database, where they can be shared by many applications.Calling a packaged subprogram for the first time loads the whole package and cachesit in memory, saving on disk I/O for subsequent calls. Thus, packages enhance reuseand improve performance in a multiuser, multi-application environment.If a subprogram does not take any parameters, you can include an empty set ofparentheses or omit the parentheses, both in PL/<strong>SQL</strong> and in functions called from <strong>SQL</strong>queries. For calls to a method that takes no parameters, an empty set of parentheses isoptional within PL/<strong>SQL</strong> scopes but required within <strong>SQL</strong> scopes.8.3.2 Accessing Variables in PackagesYou can create a package specification that is designated only to supply commonvariables to other packages or subprograms. With the variables in one package, theycan be easily maintained for all subprograms that use the variables, rather thanmaintaining the variables in all the individual subprograms. These common variablesare typically used in multiple subprograms, such as a sales tax rate.In Example 8–1, the variables my_pi and my_e can be used by any subprogram. If youchange the value of my_pi or my_e, then all subprograms that use the variable get thenew value without having to change anything in those individual subprograms.Note that you need to use of the package name as a prefix to the variable name, suchas my_pkg.my_pi.Example 8–1Using Variables in PackagesCREATE PACKAGE my_pkg ASmy_pi NUMBER := 3.14016408289008292431940027343666863227;my_e NUMBER := 2.71828182845904523536028747135266249775;my_sales_tax NUMBER := 0.0825END my_pkg;/CREATE PROCEDURE circle_area(radius NUMBER) ISmy_area NUMBER;my_datatype VARCHAR2(30);BEGINmy_area := my_pkg.my_pi * radius;DBMS_OUTPUT.PUT_LINE('Radius: ' || TO_CHAR(radius)|| ' Area: ' || TO_CHAR(my_area) );END;/-- call the circle_area procedure with radius equal to 3CALL circle_area(3);-- call the circle_area procedure with radius equal to 4BEGINcircle_area(3);END;/-- cleanup: drop package and procedureDROP PROCEDURE circle_area;DROP PACKAGE my_pkg;8-4 <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!