This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
create-package [2015/10/16 11:34] dmtolpeko created |
create-package [2015/10/16 12:13] (current) dmtolpeko |
||
---|---|---|---|
Line 8: | Line 8: | ||
<code language=sql> | <code language=sql> | ||
- | [ALTER | CREATE [OR REPLACE] | REPLACE] PACKAGE package_name AS | IS package_spec END | + | [CREATE [OR REPLACE] | REPLACE] PACKAGE package_name AS | IS package_spec END |
package_spec: | package_spec: | ||
Line 14: | Line 14: | ||
function declaration | | function declaration | | ||
procedure declaration | procedure declaration | ||
+ | </code> | ||
+ | |||
+ | Package body: | ||
+ | |||
+ | <code language=sql> | ||
+ | [CREATE [OR REPLACE] | REPLACE] PACKAGE BODY package_name AS | IS package_body END | ||
+ | |||
+ | package_body: | ||
+ | private variable declaration | | ||
+ | function definition | | ||
+ | procedure definition | ||
</code> | </code> | ||
**Example**: | **Example**: | ||
- | Define a package and then call its memebers: | + | Define a package and then call its members. |
+ | |||
+ | Package specification: | ||
<code language=sql> | <code language=sql> | ||
- | -- Package specification | ||
create or replace package users as | create or replace package users as | ||
- | total_count int := 0; | + | session_count int := 0; |
function get_count() return int; | function get_count() return int; | ||
- | procedure add_user(name varchar(100)); | + | procedure add(name varchar(100)); |
end; | end; | ||
+ | </code> | ||
+ | Package body: | ||
+ | |||
+ | <code language=sql> | ||
+ | create or replace package body users as | ||
+ | function get_count() return int | ||
+ | is | ||
+ | begin | ||
+ | return session_count; | ||
+ | end; | ||
+ | procedure add(name varchar(100)) | ||
+ | is | ||
+ | begin | ||
+ | -- ... | ||
+ | session_count = session_count + 1; | ||
+ | end; | ||
+ | end; | ||
+ | </code> | ||
+ | |||
+ | Using the package: | ||
+ | |||
+ | <code language=sql> | ||
+ | users.add('John'); | ||
+ | users.add('Sarah'); | ||
+ | users.add('Paul'); | ||
+ | print 'Number of users: ' || users.get_count(); | ||
</code> | </code> | ||