数据库

 首页 > 数据库 > postgresql > PostgreSQL函数使用介绍

PostgreSQL函数使用介绍

分享到:
【字体:
导读:
         摘要:PostgreSQL的函数也被称为存储过程,可执行操作,通常会作为一些查询和往返在一个单一的数据库内的函数。函数允许数据库重新使用其他应用程序可以直接与您的存储过程而不是一个中间层或。可以创建在所选择的语言,如SQL,PL/pgSQL,C,Python等功能语法创...

PostgreSQL函数使用介绍

PostgreSQL的函数也被称为存储过程,可执行操作,通常会作为一些查询和往返在一个单一的数据库内的函数。函数允许数据库重新使用其他应用程序可以直接与您的存储过程而不是一个中间层或。

可以创建在所选择的语言,如SQL,PL/pgSQL,C,Python等功能

语法

创建一个函数的基本语法如下:

CREATE  "pun">[OR REPLACE "pun">] "pln"> FUNCTION function_name  "pun">(arguments "pun">)
RETURNS return_datatype AS $variable_name$
  DECLARE
    declaration;
    [...]
  BEGIN
    < "pln"> function_body  "pun">>
    [...]
    RETURN { "pln"> variable_name | "pln"> value }
  END "pun">; "pln"> LANGUAGE plpgsql;

Where,

function-name specifies the name of the function.

[OR REPLACE] option allows modifying an existing function.

The function must contain a return statement.

RETURN clause specifies that data type you are going to return from the function. Thereturn_datatype can be a base, composite, or domain type, or can reference the type of a table column.

function-body contains the executable part.

The AS keyword is used for creating a standalone function.

plpgsql is the name of the language that the function is implemented in. Here we use this option for PostgreSQL, it Can be SQL, C, internal, or the name of a user-defined procedural language. For backward compatibility, the name can be enclosed by single quotes.

语法

The following example illustrates creating and calling a standalone function. This function returns the total number of records in the COMPANY table. We will use the COMPANY table, which has following records:

testdb# select * from COMPANY;
 id | name  | age | address   | salary
----+-------+-----+-----------+--------
  1 | Paul  |  32 | California|  20000
  2 | Allen |  25 | Texas     |  15000
  3 | Teddy |  23 | Norway    |  20000
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000
  6 | Kim   |  22 | South-Hall|  45000
  7 | James |  24 | Houston   |  10000
(7 rows)

Function totalRecords() is as follows:

"pln">CREATE OR REPLACE FUNCTION totalRecords  "pun">()
RETURNS integer AS $total$
declare
        total integer; "pln">
BEGIN
   SELECT count(*) "pln"> into "pln"> total FROM COMPANY "pun">;
   RETURN total;
END "pun">;
$total$ LANGUAGE plpgsql;

When the above query is executed the result would be:

testdb# CREATE FUNCTION

Now let's execute a call to this function and check the records in the COMPANY table

testdb "pun">=#  "kwd">select "pln"> totalRecords();

When the above query is executed the result would be:

 totalrecords
--------------
            7
(1 row)
PostgreSQL函数使用介绍
分享到:
15个实用的postgresql命令语句
15个实用的postgresql命令语句 piglei: 最初是想找postgresql数据库占用空间命令发现的这篇blog,发现其中提供的几 条命令很有用(但也有几条感觉是充数的=。=),于是就把它翻译过来了。另外这篇文章是09 年的,所以里面的内容可能有点过时,我收集了原文中有用的评论放在了最后面。 现在有不少开源软件都在...
在postgresql数据库中使用聚合函数
在postgresql数据库中使用聚合函数 PostgreSQL功能很强大,支持事务,嵌套SQL,触发器,聚合等等几乎所有的商业数据库的功能,不得不佩服他还免费,我个人觉得MySQL不如它。别人老说PostggreSQL慢,其实维护得好,SQL写得合理,性能和MySQL有过之而无不及。 言归正传,我前段时间用PostgreSQL来做一个功能,就是想把某...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……