博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PL/SQL Function
阅读量:6931 次
发布时间:2019-06-27

本文共 2760 字,大约阅读时间需要 9 分钟。

- A function is a named PL/SQL block that returns a value.

- A function can be stored in the database as a schema object for repeated execution.

- A function is called as part of an expression.

A function is named PL/SQL block that can accept parameters and be invoked. Generally speaking, you use a function to compute a value. Functions and procedures are structured alike. A function must have a RETURN clause in the header and at least one RETURN statement in the executable section.

Function is called as part of a SQL expression or as part of a PL/SQL expression.

Syntax for Creating Functions

CREATE [OR REPLACE] FUNCTION function_name

[( parameter1[mode1] datatype1, parameter2[mode2] datatype2 …)]

RETURN datatype  // 只是 datatype , 不需要指定具体,只要类型

IS | AS

PL/SQL Block;   ( 必须包含一个 RETURN 语句, 只返回 1 个值 )

SHOW ERRORS (可以查看错误,同时也可以在 procedure 等使用 )

例子 :

Function 的用法 与 procedure 不同,一般不应该使用 OUT, 因为一般都是调用 Function 计算某些事情,返回一个结果。

Although the three parameter modes, IN(the default), OUT, and IN OUT, can be used with any subprogram, avoid using the OUT and IN OUT modes with functions. The purpose of a function is to accept zero or more arguments(actual parameters) and return a single value.

Executing Functions ( 执行 Function )

Function 好处

- 扩展了 SQL , 比如某个地方过于复杂的 SQL , 使用一个 function 代替

- 编译好后,每次执行,效率提高

- 增加了 数据独立性,只要知道执行函数就可以了,不必知道内部接口

调用 Function

Locations to Call User-Defined Functions

- Select list of a SELECT command

- Condition of the WHERE and HAVING clauses

- CONNECT BY, START WITH, ORDER BY and GROUP BY clauses

- VALUES calues of the INSERT command

- SET clause of the UPDATE command

1: SELECT employee_id, tax(salary)
2: FROM employees
3: WHERE tax( salary ) > ( SELECT MAX( tax(salary))
4: FROM employees WHERE departmentd_id = 30 )
5: ORDER BY tax( salary ) DESC ;

Only stored functions are called from SQL statements. Stored procedures cannot be called.

Function 限制

  • When called from a SELECT statement or a parallelized UPDATE or DELETE statement, the function cannot modify any database tables
  • When called from an UPDATE, or DELETE statement, the function cannot query or modify any database tables modified by that statement.
  • When called from a SELECT, INSERT, UPDATE, or DELETE statement, the function cannot execute SQL transaction control statements( such as COMMIT ), session control statements ( such as SET ROLE ), or system control statement ( such as ALTER SYSTEM ), Also, it cannot execute DDL statements( such as CREATE) because they are followed by an automatic commit.
  • The function cannot call another subprogram that breaks one of the above restrictions.

Removing Functions

DROP FUNCTION function_name

例如 : DROP FUNCTION get_sal;

( The CREATE OR REPLACE ) syntax is equivalent to dropping a function and recreating it. ( 权限保留 )

转载地址:http://yymjl.baihongyu.com/

你可能感兴趣的文章
Centos6.5快速配置可用网卡
查看>>
mvc框架模式
查看>>
vc 串口精灵软件分析
查看>>
C#结构体和字节数组的转换
查看>>
使用Node.js搭建静态资源服务器
查看>>
bluetooth service uuid
查看>>
Android组件安全
查看>>
DOM节点属性
查看>>
Eclipse对printf()不能输出到控制台的解决方法
查看>>
【转】Open Live Writer 插件更新
查看>>
Spring Security验证流程剖析及自定义验证方法
查看>>
单链表的创建,查找,删除,插入。
查看>>
Java知识积累——Properties类的使用示例
查看>>
转:分布式ID生成方法
查看>>
python学习---装饰器
查看>>
Facial Feature Detection
查看>>
例题6-17 UVa10562 Undraw the Trees(树)
查看>>
jq屏蔽鼠标右键复制,粘贴,选中功能
查看>>
常见常数优化以及代码风格
查看>>
音频特征提取——pyAudioAnalysis工具包
查看>>