SQL函数是执行SQL语句的一个列表,返回列表中的最后一个查询结果。在简单(non-set)情况下,将返回最后一个结果的第一行。如果最后一个查询完全不返回行,则返回空值。
或者,可以声明SQL函数返回一个集合,通过将函数返回的类型指定为SETOF类型,或将其生命为返回表(列),在这种情况下返回最后一个查询结果的所有行。
SQL函数的主体必须是用分号分隔的SQL语句的列表。最后一个语句之后的分好是可选的。除非声明函数返回空,否则最后一个语句必须是具有返回子句的select、insert、update或delete。
创建实验表,向实验表中插入数据
mytest=# create table test (id serial primary key, col1 varchar(20), col2 timestamp without time zone default now());
CREATE TABLE
mytest=# create or replace function insert_test(x int)
mytest-# returns void as $$
mytest$# declare i int;
mytest$# begin
mytest$# i:=1;
mytest$# for i in 1..x loop
mytest$# insert into test(col1) values( repeat('abc',2));
mytest$# i = i+1;
mytest$# end loop;
mytest$# end;
mytest$# $$ language plpgsql;
CREATE FUNCTION
mytest=# select insert_test(100);
insert_test
-------------
(1 row)
利用Function,删除、更改数据
mytest=# CREATE OR REPLACE FUNCTION clean_test(x int) RETURNS void AS $$
mytest$# DELETE FROM test WHERE id <= x;
mytest$# $$ LANGUAGE SQL;
CREATE FUNCTION
mytest=# select clean_test(20);
clean_test
------------
(1 row)
mytest=# select * from test where id < 30;
id | col1 | col2
----+--------+----------------------------
21 | abcabc | 2018-08-29 08:03:18.457982
......
29 | abcabc | 2018-08-29 08:03:18.457982
(9 rows)
mytest=# CREATE OR REPLACE FUNCTION update_test(x int) RETURNS void AS $$
mytest$# UPDATE test SET col1 = 'abcdefg' where id <= x;
mytest$# $$ LANGUAGE SQL;
CREATE FUNCTION
mytest=# select update_test(50);
update_test
-------------
(1 row)
mytest=# select * from test where id < 60;
id | col1 | col2
----+---------+----------------------------
51 | abcabc | 2018-08-29 08:03:18.457982
......
59 | abcabc | 2018-08-29 08:03:18.457982
21 | abcdefg | 2018-08-29 08:03:18.457982
......
50 | abcdefg | 2018-08-29 08:03:18.457982
(39 rows)
通过Function查询表
mytest=# CREATE OR REPLACE FUNCTION select_test(x int) RETURNS test AS $$
mytest$# SELECT id, col1, col2 FROM test where id <= x;
mytest$# $$ LANGUAGE SQL;
CREATE FUNCTION
mytest=# select select_test(30);
select_test
-------------------------------------------
(21,abcdefg,"2018-08-29 08:03:18.457982")
(1 row)
mytest=# select * from select_test(30);
id | col1 | col2
----+---------+----------------------------
21 | abcdefg | 2018-08-29 08:03:18.457982
(1 row)
mytest=# CREATE OR REPLACE FUNCTION select_setof_test(x int) RETURNS SETOF test AS $$
mytest$# SELECT id, col1, col2 FROM test where id <= x;
mytest$# $$ LANGUAGE SQL;
CREATE FUNCTION
mytest=# select select_setof_test(30);
select_setof_test
-------------------------------------------
(21,abcdefg,"2018-08-29 08:03:18.457982")
......
(30,abcdefg,"2018-08-29 08:03:18.457982")
(10 rows)
mytest=# select * from select_setof_test(30) ;
id | col1 | col2
----+---------+----------------------------
21 | abcdefg | 2018-08-29 08:03:18.457982
......
30 | abcdefg | 2018-08-29 08:03:18.457982
(10 rows)
查看执行计划
mytest=# explain analyze select * from select_setof_test(30) ;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
Function Scan on select_setof_test (cost=0.25..10.25 rows=1000 width=70) (actual time=0.541..0.573 rows=10 loops=1)
Planning time: 0.099 ms
Execution time: 0.658 ms
(3 rows)
mytest=# explain analyze select * from test where id <= 30;
QUERY PLAN
-------------------------------------------------------------------------------------------------
Seq Scan on test (cost=0.00..4.50 rows=30 width=19) (actual time=0.140..0.194 rows=10 loops=1)
Filter: (id <= 30)
Rows Removed by Filter: 170
Planning time: 0.209 ms
Execution time: 0.282 ms
(5 rows)