gpt4 book ai didi

postgresql - 在 PostgreSQL 中,PL/pgSQL 和 SQL 有不同的 CREATE FUNCTION 命令吗?

转载 作者:行者123 更新时间:2023-11-29 12:16:33 27 4
gpt4 key购买 nike

在 PostgreSQL 文档中,创建 PL/pgSQL 函数的命令和创建 SQL 函数的命令看起来非常相似但有区别:

Functions written in PL/pgSQL are defined to the server by executing CREATE FUNCTION commands. Such a command would normally look like, say,

CREATE FUNCTION somefunc(integer, text) RETURNS integer
AS 'function body text'
LANGUAGE plpgsql;

The function body is simply a string literal so far as CREATE FUNCTION is concerned. It is often helpful to use dollar quoting (see Section 4.1.2.4) to write the function body, rather than the normal single quote syntax. Without dollar quoting, any single quotes or backslashes in the function body must be escaped by doubling them. Almost all the examples in this chapter use dollar-quoted literals for their function bodies. PL/pgSQL is a block-structured language. The complete text of a function body must be a block. A block is defined as:

[ <<label>> ]
[ DECLARE
declarations ]
BEGIN
statements
END [ label ];

在SQL命令列表中:

CREATE [ OR REPLACE ] FUNCTION
name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT |
= } default_expr ] [, ...] ] )
[ RETURNS rettype
| RETURNS TABLE ( column_name column_type [, ...] ) ]
{ LANGUAGE lang_name
| TRANSFORM { FOR TYPE type_name } [, ... ]
| WINDOW
| IMMUTABLE | STABLE | VOLATILE | [ NOT ] LEAKPROOF
| CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT
| [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER
| PARALLEL { UNSAFE | RESTRICTED | SAFE }
| COST execution_cost
| ROWS result_rows
| SET configuration_parameter { TO value | = value | FROM
CURRENT }
| AS 'definition'
| AS 'obj_file', 'link_symbol'
} ...
[ WITH ( attribute [, ...] ) ]

比如我们可以指定一种语言

  • 通过 LANGUAGE ... 在 PL/pgSQL 命令的末尾和

  • 通过 SQL 命令中的 LANGUAGE lang_name

它们真的是两个不同的命令吗?或者它们是相同的命令?

谢谢。

最佳答案

CREATE FUNCTION 的引用定义对于所有语言都是通用的:

lang_name

The name of the language that the function is implemented in. It can be sql, c, internal, or the name of a user-defined procedural language, e.g. plpgsql.

关于postgresql - 在 PostgreSQL 中,PL/pgSQL 和 SQL 有不同的 CREATE FUNCTION 命令吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51072492/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com