内联型 SQL 函数和编译型 SQL 函数
SQL 函数有两种实现类型:内联型 SQL 函数和编译型 SQL 函数。
内联型 SQL 函数通常将获得最佳性能。但是,它们不支持编译型 SQL 函数支持的某些 SQL PL 语言功能部件和数据类型。这些功能部件包括:
- CASE 语句
- REPEAT 语句
- 游标处理
- 动态 SQL
- 条件处理程序
在 SQL PL 模块中已声明的 SQL 函数始终是编译型 SQL 函数。
PL/SQL 函数始终是编译型 PL/SQL 函数。虽然在 PL/SQL 代码中可以引用内联型函数,但是无法使用 PL/SQL 来声明这些函数。必须使用 SQL PL 来声明这些函数。
对于所有其他情况,CREATE FUNCTION 语句的语法将确定 SQL 函数是内联型函数还是编译型函数。
- 具有原子主体的 SQL PL 函数是内联型函数。如果一个 SQL 函数包含单个 RETURN 语句,或者由一个以 BEGIN ATOMIC 关键字开头的复合语句组成,那么该 SQL 函数是原子函数。
- 非原子函数的 SQL PL 函数是编译型函数。如果一个 SQL PL 函数包含以 BEGIN 或 BEGIN NOT ATOMIC 关键字开头的复合语句,那么该 SQL PL 函数为非原子函数。
示例
- 内联型函数
create function F1 (N integer) returns integer deterministic no external action contains sql return N * 10 @create function F2 (N integer) returns integer deterministic no external action contains sql begin atomic if N < 5 then return N * 10 else return N * 20 end if; end @ - 编译型函数
create function F3 (N integer) returns integer deterministic no external action contains sql begin if N < 5 then return N * 10 else return N * 20 end if; end @create function F4 (N integer) returns integer deterministic no external action contains sql begin not atomic if N < 5 then return N * 10 else return N * 20 end if; end @