Oracle Database(6) PCK

Stored Subprograms

A subprogram is a PL/SQL unit that consists of SQL and PL/SQL statements that solve a specific problem or perform a set of related tasks. A subprogram can have parameters, whose values are supplied by the invoker. A subprogram can be either a procedure or a function. Typically, you use a procedure to perform an action and a function to compute and return a value.
A stored subprogram is a subprogram that is stored in the database. Because they are stored in the database, stored programs can be used as building blocks for many different database applications.
There are two kinds of stored subprograms:

  • Standalone subprogram, which is created at schema level
  • Package subprogram, which is created inside a package.

Standalone subprograms are useful for testing pieces of program logic, but when you are sure that they work as intended, Oracle recommends that you put them into packages.

About Packages

A package is a PL/SQL unit that consists of related subprograms and that declared cursors and variables that they use.
Oracle recommends that you put your subprograms into packages. Some reasons are:

  • Packages allow you to hide implementation details from client programs.
  • Package subprograms must be qualified with package names when invoked from outside the package, which ensures that their names will always work when invoked from outside the package.
About Subprogram Structure

A subprogram follows PL/SQL block structure, it has

  • Declarative part (optional)
    The declarative part contains declarations of types, constants, variables, exceptions, declared cursors, and nested subprograms. These items are local to the subprogram and cease to exist when the subprogram completes execution.
  • Executable part (required)
    The executable part contains statements that assign values, control execution, and manipulate data.
  • Exception-handling part (optional)
    The exception-handling part contains code that handles exceptions.

Comments can appear anywhere in PL/SQL code. The PL/SQL compiler ignores them. Adding comments to your program promotes readability and aids understanding, A single-ling comment start with a -- and extends to the end of the line. A multiline comment starts with a /* and ends with a */
The structure of a procedure is:

PROCEDURE name [ ( parameter_list ) ]
{ IS | AS }
  [ declarative_part ]
BEGIN -- executable part begins
  statement; [ statement; ] ...
[ EXCEPTION -- executable part ends, exception-handling part begins]
  exception_handler; [ exception_handler; ] ... 
END;

The structure of a function is like that of a procedure, except that it includes a RETURN clause and at least one RETURN statement.

FUNCTION name [ ( parameter_list ) ] RETURN data_type [ clauses ]
{ IS | AS }
  [ declarative_part ]
BEGIN -- executable part begins
  -- at least one statement must be a RETURN statement
  statement; [ statement; ] ...
[ EXCEPTION -- executable part ends, exception-handling part begins]
  exception_handler; 
END;
About Package Structure

A package always has a specification, and it usually has a body.
The package specification defines the package, declaring the types, variables, constants, exceptions, declared cursors, and subprograms that can be referenced from outside the package. A package specification is an application program interface(API). It has all the information that client programs need to invoke its subprograms, but no information about their implementation.
The package body defines the queries for the declared cursors, and the code for the subprograms, that are declared in the package specification.
The package body can also define local subprograms, which are not declared in the specification and can be invoked only be other subprograms in the package.
Package body contents are hidden from client programs. You can change the package body without invalidating the applications that call the package.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,867評(píng)論 0 10
  • 001 水牛犁地深,所以泥土都是底層休養(yǎng)過的土,肥力大,稻子生長(zhǎng)更好。 要想向上長(zhǎng)得好,首先就要低下頭先去挖掘底層...
    亢奮的蘑菇閱讀 401評(píng)論 0 2
  • 昨晚終于看完電視劇《玄門大師》,這樣的仙俠玄幻劇是自己喜歡的,又找來(lái)小說(shuō)在看,今天就先簡(jiǎn)單記錄一下: 首先電視劇講...
    四夕清荷閱讀 721評(píng)論 0 0
  • 距離上一篇文章發(fā)布已經(jīng)快 兩 三個(gè)月了,本來(lái)早就想再發(fā)一篇比較干貨點(diǎn)的文章搞個(gè)大新聞,但是無(wú)奈上個(gè)月各種事情纏身,...
    RunningBun閱讀 797評(píng)論 0 4
  • 秋風(fēng)清,秋月明,落葉聚還散,寒鴉棲復(fù)驚。 相親相見知何日,此時(shí)此 夜難為情; 入我相思門,知我相思苦,長(zhǎng)相思兮長(zhǎng)相...
    妲再美終是妾閱讀 322評(píng)論 0 0

友情鏈接更多精彩內(nèi)容