Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Greenplum
Gpdb
提交
4e64e7f5
G
Gpdb
项目概览
Greenplum
/
Gpdb
通知
7
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
Gpdb
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4e64e7f5
编写于
1月 07, 2005
作者:
T
Tom Lane
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve discussion of SQL functions taking/returning row types.
上级
7a986fb4
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
81 addition
and
41 deletion
+81
-41
doc/src/sgml/xfunc.sgml
doc/src/sgml/xfunc.sgml
+81
-41
未找到文件。
doc/src/sgml/xfunc.sgml
浏览文件 @
4e64e7f5
<!--
<!--
$PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.9
2 2004/12/30 21:45:37
tgl Exp $
$PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.9
3 2005/01/07 22:40:46
tgl Exp $
-->
-->
<sect1 id="xfunc">
<sect1 id="xfunc">
...
@@ -111,6 +111,39 @@ $PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.92 2004/12/30 21:45:37 tgl Exp $
...
@@ -111,6 +111,39 @@ $PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.92 2004/12/30 21:45:37 tgl Exp $
<type>void</>, the last statement must be a <command>SELECT</>.
<type>void</>, the last statement must be a <command>SELECT</>.
</para>
</para>
<para>
Any collection of commands in the <acronym>SQL</acronym>
language can be packaged together and defined as a function.
Besides <command>SELECT</command> queries, the commands can include data
modification queries (<command>INSERT</command>,
<command>UPDATE</command>, and <command>DELETE</command>), as well as
other SQL commands. (The only exception is that you can't put
<command>BEGIN</>, <command>COMMIT</>, <command>ROLLBACK</>, or
<command>SAVEPOINT</> commands into a <acronym>SQL</acronym> function.)
However, the final command
must be a <command>SELECT</command> that returns whatever is
specified as the function's return type. Alternatively, if you
want to define a SQL function that performs actions but has no
useful value to return, you can define it as returning <type>void</>.
In that case, the function body must not end with a <command>SELECT</command>.
For example, this function removes rows with negative salaries from
the <literal>emp</> table:
<screen>
CREATE FUNCTION clean_emp() RETURNS void AS '
DELETE FROM emp
WHERE salary < 0;
' LANGUAGE SQL;
SELECT clean_emp();
clean_emp
-----------
(1 row)
</screen>
</para>
<para>
<para>
The syntax of the <command>CREATE FUNCTION</command> command requires
The syntax of the <command>CREATE FUNCTION</command> command requires
the function body to be written as a string constant. It is usually
the function body to be written as a string constant. It is usually
...
@@ -219,35 +252,6 @@ $$ LANGUAGE SQL;
...
@@ -219,35 +252,6 @@ $$ LANGUAGE SQL;
which adjusts the balance and returns the new balance.
which adjusts the balance and returns the new balance.
</para>
</para>
<para>
Any collection of commands in the <acronym>SQL</acronym>
language can be packaged together and defined as a function.
Besides <command>SELECT</command> queries,
the commands can include data modification (i.e.,
<command>INSERT</command>, <command>UPDATE</command>, and
<command>DELETE</command>). However, the final command
must be a <command>SELECT</command> that returns whatever is
specified as the function's return type. Alternatively, if you
want to define a SQL function that performs actions but has no
useful value to return, you can define it as returning <type>void</>.
In that case, the function body must not end with a <command>SELECT</command>.
For example:
<screen>
CREATE FUNCTION clean_emp() RETURNS void AS $$
DELETE FROM emp
WHERE salary <= 0;
$$ LANGUAGE SQL;
SELECT clean_emp();
clean_emp
-----------
(1 row)
</screen>
</para>
</sect2>
</sect2>
<sect2>
<sect2>
...
@@ -282,7 +286,7 @@ SELECT name, double_salary(emp.*) AS dream
...
@@ -282,7 +286,7 @@ SELECT name, double_salary(emp.*) AS dream
name | dream
name | dream
------+-------
------+-------
Sam | 2
400
Bill | 8
400
</screen>
</screen>
</para>
</para>
...
@@ -307,7 +311,7 @@ SELECT name, double_salary(emp) AS dream
...
@@ -307,7 +311,7 @@ SELECT name, double_salary(emp) AS dream
on-the-fly. This can be done with the <literal>ROW</> construct.
on-the-fly. This can be done with the <literal>ROW</> construct.
For example, we could adjust the data being passed to the function:
For example, we could adjust the data being passed to the function:
<screen>
<screen>
SELECT name, double_salary(
row
(name, salary*1.1, age, cubicle)) AS dream
SELECT name, double_salary(
ROW
(name, salary*1.1, age, cubicle)) AS dream
FROM emp;
FROM emp;
</screen>
</screen>
</para>
</para>
...
@@ -320,7 +324,7 @@ SELECT name, double_salary(row(name, salary*1.1, age, cubicle)) AS dream
...
@@ -320,7 +324,7 @@ SELECT name, double_salary(row(name, salary*1.1, age, cubicle)) AS dream
<programlisting>
<programlisting>
CREATE FUNCTION new_emp() RETURNS emp AS $$
CREATE FUNCTION new_emp() RETURNS emp AS $$
SELECT text 'None' AS name,
SELECT text 'None' AS name,
1000 AS salary,
1000
.0
AS salary,
25 AS age,
25 AS age,
point '(2,2)' AS cubicle;
point '(2,2)' AS cubicle;
$$ LANGUAGE SQL;
$$ LANGUAGE SQL;
...
@@ -358,9 +362,46 @@ ERROR: function declared to return emp returns varchar instead of text at colum
...
@@ -358,9 +362,46 @@ ERROR: function declared to return emp returns varchar instead of text at colum
</para>
</para>
<para>
<para>
When you call a function that returns a row (composite type) in a
A different way to define the same function is:
SQL expression, you might want only one field (attribute) from its
result. You can do that with syntax like this:
<programlisting>
CREATE FUNCTION new_emp() RETURNS emp AS $$
SELECT ROW('None', 1000.0, 25, '(2,2)')::emp;
$$ LANGUAGE SQL;
</programlisting>
Here we wrote a <command>SELECT</> that returns just a single
column of the correct composite type. This isn't really better
in this situation, but it is a handy alternative in some cases
— for example, if we need to compute the result by calling
another function that returns the desired composite value.
</para>
<para>
We could call this function directly in either of two ways:
<screen>
SELECT new_emp();
new_emp
--------------------------
(None,1000.0,25,"(2,2)")
SELECT * FROM new_emp();
name | salary | age | cubicle
------+--------+-----+---------
None | 1000.0 | 25 | (2,2)
</screen>
The second way is described more fully in <xref
linkend="xfunc-sql-table-functions">.
</para>
<para>
When you use a function that returns a composite type,
you might want only one field (attribute) from its result.
You can do that with syntax like this:
<screen>
<screen>
SELECT (new_emp()).name;
SELECT (new_emp()).name;
...
@@ -398,15 +439,14 @@ SELECT name(new_emp());
...
@@ -398,15 +439,14 @@ SELECT name(new_emp());
<screen>
<screen>
-- This is the same as:
-- This is the same as:
-- SELECT emp.name AS youngster FROM emp WHERE emp.age < 30
-- SELECT emp.name AS youngster FROM emp WHERE emp.age < 30
;
SELECT name(emp) AS youngster
SELECT name(emp) AS youngster FROM emp WHERE age(emp) < 30;
FROM emp
WHERE age(emp) < 30;
youngster
youngster
-----------
-----------
Sam
Sam
Andy
</screen>
</screen>
</para>
</para>
...
@@ -433,7 +473,7 @@ SELECT getname(new_emp());
...
@@ -433,7 +473,7 @@ SELECT getname(new_emp());
</para>
</para>
</sect2>
</sect2>
<sect2>
<sect2
id="xfunc-sql-table-functions"
>
<title><acronym>SQL</acronym> Functions as Table Sources</title>
<title><acronym>SQL</acronym> Functions as Table Sources</title>
<para>
<para>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录