PL/pgSQL的RETURN QUERY例子】的更多相关文章

我的例子: 数据准备: create table custinfo(custid integer,callingcnt integer); ,),(,),(,); 函数生成: CREATE OR REPLACE FUNCTION get_callingcnt(custid int) RETURNS TABLE ( custid int ,callingcnt int ) AS $$ BEGIN RETURN QUERY SELECT t.custid ,t.callingcnt FROM cus…
从网上找到例子: 可以说,RETURN NEXT要用在循环中: 例子一: 数据准备: CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT); , , 'three'); , , 'six'); 生成函数: CREATE OR REPLACE FUNCTION getAllFoo() RETURNS SETOF foo AS $BODY$ DECLARE r foo%rowtype; BEGIN FOR r IN SELECT * FRO…
例子一,不带returns: postgres=# CREATE FUNCTION sum_n_product(x int, y int, OUT sum int, OUT prod int) AS $$ postgres$# BEGIN postgres$# sum := x + y; postgres$# prod := x * y; postgres$# END; postgres$# $$ LANGUAGE plpgsql; CREATE FUNCTION postgres=# post…
实验如下: RETURNS TABLE 中的变量名和SQL文中的变量名同名时,执行时会出错: pgsql=# create table sales(itemno integer,quantity integer,price numeric); CREATE TABLE pgsql,,,,12.3); pgsql=# CREATE FUNCTION extended_sales(p_itemno int) pgsql-# RETURNS TABLE(quantity int, total nume…
http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 另外一种声明 PL/pgSQL 函数的方法是使用 returns table,例如: CREATE FUNCTION extended_sales(p_itemno int) RETURNS TABLE(quantity int, total numeric) AS $$ BEGIN RETURN QUERY SELECT quantity, quantity…
http://www.postgresql.org/docs/9.1/static/plpgsql-overview.html 39.1.2. Supported Argument and Result Data Types 用PL/pgSQL写的函数可以接受标量类型或者服务器支持的数组类型作为参数,它们可以返回前述的数据类型的结果.它们也能接受或返回通过名称指定的复杂类型(rwo type).也可以声明一个PL/pgSQL函数返回 record类型,此时返回的结果是一个row type,它的列…
http://blog.chinaunix.net/uid-7591044-id-1742967.html 今天学会了用 PL/pgSQL 写 postgreSQL 的存储过程,网上资料实在少得可怜,唯一能搜到的一些还是抄来抄去的:还是翻postgresql的文档吧,把今天解决的问题说一下吧,希望对其他人有帮助.问题是这样的,有一张message表:CREATE TABLE message(id int8 NOT NULL,receiveuserid int8,senduserid int8,r…
http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 如果一个PL/pgSQL函数声明了输出参数,输出参数被赋予$n名称和可选的别名,和正常输入参数的作法一样.输出参数是一个从NULL开始的变量:它将被在函数的执行过程中被赋值.此参数的最后的值就是函数的返回值.例如,the sales-tax 例子可以这样实现: 例子: CREATE FUNCTION sales_tax(subtotal real, OUT ta…
http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 39.3. 声明 块中使用的所有的变量必须在块的声明节中进行声明.(唯一的例外是,子一个For循环中,在一个整数范围内轮询的循环变量被自动认为是整型变量,而只For循环中,轮询一个游标的变量被自动声明为记录变量.) PL/pgSQL 变量可以是任何SQL数据类型,如integer,varchar,还有char等. 下面是变量声明的一些个例子: user_id i…
http://www.postgresql.org/docs/9.1/static/plpgsql-structure.html 39.2. PL/pgSQL 的结构 PL/pgSQL是一种块式结构的语言.完整的函数定义必须是一个块.一个块的定义形式如下: [ <<label>> ] [ DECLARE declarations ] BEGIN statements END [ label ]; 在块中,每一个声明或语句都以分号结束.一个块出现在另外一个块中的时候,必须接 END:…