Home Database Mysql Tutorial Oracle中packages的使用

Oracle中packages的使用

Jun 07, 2016 pm 05:12 PM

最近,由于开发的功能较多,对于编写的很多过程,看着英文名字觉得挺熟悉,但要看功能,则需要每个点开去开备注或者代码,觉得很

最近,由于开发的功能较多,对于编写的很多过程,看着英文名字觉得挺熟悉,但要看功能,则需要每个点开去开备注或者代码,觉得很是郁闷。突见packages,想将所有写的过程放到一起,这样下次点开一个,就能看到所有功能的过程,,很是方便。趁着下午偶闲一会,将packages小做研究。

包的作用 : 包可以将任何出现在块声明的语句 ( 过程 , 函数 , 游标 ,  变量 ) 放于包中 , 相当于一个容器 。 将声明语句放入包中的好处是 : 用户可以从其他 PL/SQL 块中对其进行引用 , 因此包为 PL/SQL 提供了全程变量 。

包分为两部分 : 包头和包体。

如何创建包

1) 包头 :

  语法格式 :

  CREATE OR REPLACE PACKAGE package_name /* 包头名称 */

  IS|AS pl/sql_package_spec                     /* 定义过程 , 函数以及返回类型 , 变量 , 常量及数据类型定义 */

  定义包头应当遵循以下原则 :

  1) 包元素位置可以任意安排 。然而在声明部分 , 对象必须在引用前进行声明 。

  2) 包头可以不对任何类型的元素进行说明 。 例如 , 包头可以只带过程和函数说明语句 , 而不声明任何异常和类型 。

  3) 对过程和函数的任何声明都必须只对子程序和其参数进行描述 , 不能有任何代码的说明 , 代码的实现只能在包体中出现 。 它不同于块声明 , 在块声明中 , 过程和函数的代码可同时出现在声明部分 。

2. 包体 :

  语法格式 :

  CREATE OR REPLACE PACKAGE BODY package_name/* 包名必须与包头的包名一致 */

  IS | AS pl/sql_package_body                    /* 游标 , 函数 , 过程的具体定义 */

包体是与包头相互独立的 , 包体只能在包头完成编译后才能进行编译 。 包体中带有包头中描述的子程序的具体实现的代码段 。 除此之外 , 包体还可以包括具有包体人全句属性的附加声明部分 , 但这些附加声明对于包头是不见的 。

EG: 定义一个包头

 

CREATE OR REPLACE PACKAGE select_table

IS

TYPE tab_02 IS RECORD

  (

  itnum_1 varchar2(1),

  itnum_2 varchar2(1)

);

TYPE tab_03 IS RECORD

(

  itnum_1 varchar2(1),

  itnum_2 varchar2(1),

  itnum_3 varchar2(1)

);

TYPE tab_04 IS RECORD

(

  itnum_1 varchar2(1),

  itnum_2 varchar2(1),

  itnum_3 varchar2(1),

  itnum_4 varchar2(1)

);

TYPE tab_05 IS RECORD

(

  itnum_1 varchar2(1),

  itnum_2 varchar2(1),

  itnum_3 varchar2(1),

  itnum_4 varchar2(1),

itnum_5 varchar2(1)

);

TYPE tab_06 IS RECORD

(

  itnum_1 varchar2(1),

  itnum_2 varchar2(1),

  itnum_3 varchar2(1),

  itnum_4 varchar2(1),

itnum_5 varchar2(1),

itnum_6 varchar2(1)

);

TYPE cur_02 IS REF CURSOR RETURN tab_02;

TYPE cur_03 IS REF CURSOR RETURN tab_03;

TYPE cur_04 IS REF CURSOR RETURN tab_04;

TYPE cur_05 IS REF CURSOR RETURN tab_05;

TYPE cur_06 IS REF CURSOR RETURN tab_06;

END select_tab;

 

 

EG:

CREATE OR REPLACE PACKAGE test_package

IS

FUNCTION average

(cnum IN char)

RETURN NUMBER;

PRODURE student_grade

(CUR OUT select_table.cur_04);--CUR 的数据类型是 select_table 包中 cur_o4

END test_package;

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

See all articles