Functions vs procedures in Oracle(Oracle 中的函数与过程)
问题描述
谁能解释一下 Oracle 中函数和过程之间的主要区别是什么?如果我可以用函数做任何事情,为什么我必须使用过程?
can anybody explain what is the main difference between functions and procedures in Oracle? Why must I use procedures if I can do everything with functions?
- 如果我不能在 sql 语句中调用过程,好吧,我会写一个函数来做同样的工作.
- 过程不返回值,好吧,我将在任何 dml 操作后仅返回 sql%rowcount 或 1(success), 0(exception)
- 过程和函数都可以通过 OUT/IN OUT 参数将变量传递给调用环境
我听说主要区别在于性能,过程比函数快".但没有任何细节.
I heard that the main difference is in performance, 'procedures are faster than functions'. But without any detail.
提前致谢.
推荐答案
不同之处在于 - 一个函数必须在默认情况下返回一个值(任何类型),而在程序的情况下,你需要使用类似的参数OUT 或 IN OUT 参数获取结果.您可以在普通 SQL 中使用函数,而不能在 SQL 语句中使用过程.
The difference is- A function must return a value (of any type) by default definition of it, whereas in case of a procedure you need to use parameters like OUT or IN OUT parameters to get the results. You can use a function in a normal SQL where as you cannot use a procedure in SQL statements.
函数和过程之间的一些差异
Some Differences between Functions and Procedures
一个函数总是使用 return 语句返回一个值,而一个过程可能通过参数返回一个或多个值,也可能根本不返回.尽管
OUT参数仍然可以用于功能,它们是不可取的,也没有人可能发现需要这样做的情况.使用OUT参数限制函数在 SQL 语句中使用.
A function always returns a value using the return statement while a procedure may return one or more values through parameters or may not return at all.Although,
OUTparameters can still be used in functions, they are not advisable neither are there cases where one might find a need to do so. UsingOUTparameter restricts a function from being used in a SQL Statement.
函数可用于典型的 SQL 语句,如 SELECT、INSERT、UPDATE、DELETE, MERGE,而过程不能.
Functions can be used in typical SQL statements like SELECT, INSERT, UPDATE, DELETE, MERGE, while procedures can't.
函数通常用于计算,而过程通常用于执行业务逻辑.
Functions are normally used for computations where as procedures are normally used for executing business logic.
Oracle 提供了创建基于函数的索引"的规定提高后续 SQL 语句的性能.这适用于在查询的 where 子句中对索引列执行函数时.
Oracle provides the provision of creating "Function Based Indexes" to improve the performance of the subsequent SQL statement. This applies when performing the function on an indexed column in where clause of a query.
关于函数与函数的更多信息过程此处和这里.
More Information on Functions Vs. Procedures here and here.
这篇关于Oracle 中的函数与过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Oracle 中的函数与过程
基础教程推荐
- 无法解决整理冲突 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
