函数和语句之间的区别

时间:2022-09-06 13:17:19

In class we were asked to name several PL/SQL functions and state their use. Someone mentioned CREATE FUNCTION and I noted that Oracle labeled this as a statement and not a function.

在课堂上,我们被要求命名几个PL / SQL函数并说明它们的用法。有人提到了CREATE FUNCTION,我注意到Oracle将此标记为语句而不是函数。

I am unable to give a very good answer on the difference between the two. I am thinking that a statement is more of a declarative tool that specifies what the program is to accomplish whereas a function seems more procedural, we define the procedure for a new function in the CREATE FUNCTION statement.

我无法就两者之间的差异给出一个很好的答案。我认为语句更多的是一个声明性工具,它指定了程序要完成的内容,而函数似乎更具程序性,我们在CREATE FUNCTION语句中定义新函数的过程。

The distinction seems clearer for something like a SELECT statement, can someone help with a clear delimitation between the two?

像SELECT语句这样的区别似乎更清楚,有人可以帮助明确界定两者吗?

3 个解决方案

#1


A statement is a piece of code that executes. It is a command. It does something. Example:

语句是一段执行的代码。这是一个命令。它做了一些事情。例:

print "Hello World"

打印“Hello World”

A function is a block of code that you call with parameters and it returns a value (even if that value is undefined). Example:

函数是使用参数调用的代码块,它返回一个值(即使该值未定义)。例:

function(a) { return a * 2 }

函数(a){return a * 2}

#2


CREATE FUNCTION is a statement. The function itself is a FUNCTION. Running the statement that creates the function allows you to access the function.

CREATE FUNCTION是一个声明。函数本身就是一个功能。运行创建该函数的语句允许您访问该函数。

In general, if you're creating anything in the database, you are using a statement (in particular, a data-definition language (DDL) statement). The item you create, be it a table, function, package, etc., can then be referenced in future statements.

通常,如果要在数据库中创建任何内容,则使用语句(特别是数据定义语言(DDL)语句)。您创建的项目,无论是表,函数,包等,都可以在将来的语句中引用。

#3


The word "statement" has two different meanings in SQL and PL/SQL.

“语句”一词在SQL和PL / SQL中有两种不同的含义。

In a narrow technical sense, CREATE FUNCTION is a type of SQL statement, and CREATE FUNCTION contains PL/SQL statements.

从狭义的技术意义上讲,CREATE FUNCTION是一种SQL语句,CREATE FUNCTION包含PL / SQL语句。

In the more general sense, a SQL statement is declarative code that tells the database "what is true" or "what has to be done", without detailed instructions on "how to do it". A function, or any PL/SQL block, is imperative code that tells the database exactly "what to do". Declarative SQL is usually simpler and faster than imperative PL/SQL so it's usually best to do most of the work in SQL and just glue it together with PL/SQL.

在更一般的意义上,SQL语句是声明性代码,它告诉数据库“什么是真”或“必须做什么”,没有关于“如何做”的详细说明。函数或任何PL / SQL块是必需的代码,它告诉数据库确切的“做什么”。声明性SQL通常比命令式PL / SQL更简单,更快速,因此通常最好在SQL中完成大部分工作,并将其与PL / SQL粘合在一起。


SQL has about 170 commands (as defined by V$SQLCOMMAND), organized into about 31 statement types, organized into the below 6 categories. The CREATE FUNCTION command is a CREATE statement type in the DDL category. That classification scheme is not 100% clear from the database and manuals, but I've used it to thoroughly classify all SQL statements.

SQL有大约170个命令(由V $ SQLCOMMAND定义),组织成大约31种语句类型,组织成以下6个类别。 CREATE FUNCTION命令是DDL类别中的CREATE语句类型。数据库和手册并没有100%明确这种分类方案,但我用它来彻底分类所有SQL语句。

Data Definition Language (DDL) Statements

Data Manipulation Language (DML) Statements

Transaction Control Statements

Session Control Statements

System Control Statement

Embedded SQL Statements

In PL/SQL, most blocks are a sequence of statements. These statements could be an assignment, loop, fetch, block, etc.

在PL / SQL中,大多数块是一系列语句。这些语句可以是赋值,循环,提取,块等。

函数和语句之间的区别


To make things more confusing, in 12c a SQL statement can contain PL/SQL statements:

为了使事情更加混乱,在12c中,SQL语句可以包含PL / SQL语句:

with function f return number is begin return 1; end; select f from dual;

#1


A statement is a piece of code that executes. It is a command. It does something. Example:

语句是一段执行的代码。这是一个命令。它做了一些事情。例:

print "Hello World"

打印“Hello World”

A function is a block of code that you call with parameters and it returns a value (even if that value is undefined). Example:

函数是使用参数调用的代码块,它返回一个值(即使该值未定义)。例:

function(a) { return a * 2 }

函数(a){return a * 2}

#2


CREATE FUNCTION is a statement. The function itself is a FUNCTION. Running the statement that creates the function allows you to access the function.

CREATE FUNCTION是一个声明。函数本身就是一个功能。运行创建该函数的语句允许您访问该函数。

In general, if you're creating anything in the database, you are using a statement (in particular, a data-definition language (DDL) statement). The item you create, be it a table, function, package, etc., can then be referenced in future statements.

通常,如果要在数据库中创建任何内容,则使用语句(特别是数据定义语言(DDL)语句)。您创建的项目,无论是表,函数,包等,都可以在将来的语句中引用。

#3


The word "statement" has two different meanings in SQL and PL/SQL.

“语句”一词在SQL和PL / SQL中有两种不同的含义。

In a narrow technical sense, CREATE FUNCTION is a type of SQL statement, and CREATE FUNCTION contains PL/SQL statements.

从狭义的技术意义上讲,CREATE FUNCTION是一种SQL语句,CREATE FUNCTION包含PL / SQL语句。

In the more general sense, a SQL statement is declarative code that tells the database "what is true" or "what has to be done", without detailed instructions on "how to do it". A function, or any PL/SQL block, is imperative code that tells the database exactly "what to do". Declarative SQL is usually simpler and faster than imperative PL/SQL so it's usually best to do most of the work in SQL and just glue it together with PL/SQL.

在更一般的意义上,SQL语句是声明性代码,它告诉数据库“什么是真”或“必须做什么”,没有关于“如何做”的详细说明。函数或任何PL / SQL块是必需的代码,它告诉数据库确切的“做什么”。声明性SQL通常比命令式PL / SQL更简单,更快速,因此通常最好在SQL中完成大部分工作,并将其与PL / SQL粘合在一起。


SQL has about 170 commands (as defined by V$SQLCOMMAND), organized into about 31 statement types, organized into the below 6 categories. The CREATE FUNCTION command is a CREATE statement type in the DDL category. That classification scheme is not 100% clear from the database and manuals, but I've used it to thoroughly classify all SQL statements.

SQL有大约170个命令(由V $ SQLCOMMAND定义),组织成大约31种语句类型,组织成以下6个类别。 CREATE FUNCTION命令是DDL类别中的CREATE语句类型。数据库和手册并没有100%明确这种分类方案,但我用它来彻底分类所有SQL语句。

Data Definition Language (DDL) Statements

Data Manipulation Language (DML) Statements

Transaction Control Statements

Session Control Statements

System Control Statement

Embedded SQL Statements

In PL/SQL, most blocks are a sequence of statements. These statements could be an assignment, loop, fetch, block, etc.

在PL / SQL中,大多数块是一系列语句。这些语句可以是赋值,循环,提取,块等。

函数和语句之间的区别


To make things more confusing, in 12c a SQL statement can contain PL/SQL statements:

为了使事情更加混乱,在12c中,SQL语句可以包含PL / SQL语句:

with function f return number is begin return 1; end; select f from dual;