http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html

39.3.1. 声明函数参数

传递给函数的参数被用 $1、$2等依次类推的标志符命名。作为可选项,为了提高可读性,可以为$n 参数名称定义别名。此后,既可以用数字标志符也可以用别名来指代参数值。

有两种方式来创建别名。推荐使用的方法是在CREATE FUNCTION命令你个中给参数一个名字,例如:

CREATE FUNCTION sales_tax(subtotal real) RETURNS real AS $$
BEGIN
RETURN subtotal * 0.06;
END;
$$ LANGUAGE plpgsql;

另外一种方法,在PostgreSQL8.0之前,是唯一的方法,它显式声明一个别名。采用如下的声明语法:
name ALIAS FOR $n;

上述例子采用这种方式后变为:

CREATE FUNCTION sales_tax(real) RETURNS real AS $$
DECLARE
subtotal ALIAS FOR $1;
BEGIN
RETURN subtotal * 0.06;
END;
$$ LANGUAGE plpgsql;

请注意: 上述两个例子并不是完全相同。在第一个例子中, subtotal 可以通过 sales_tax.subtotal 来参照。但是在第二个例子中不能如此。 (如果在内部块中给一个标签,倒是可以用 标签.subtotal方式来指定它)

更多的例子:

CREATE FUNCTION instr(varchar, integer) RETURNS integer AS $$
DECLARE
v_string ALIAS FOR $1;
index ALIAS FOR $2;
BEGIN
-- some computations using v_string and index here
END;
$$ LANGUAGE plpgsql; CREATE FUNCTION concat_selected_fields(in_t sometablename) RETURNS text AS $$
BEGIN
RETURN in_t.f1 || in_t.f3 || in_t.f5 || in_t.f7;
END;
$$ LANGUAGE plpgsql;

PL/pgSQL学习笔记之六的更多相关文章

  1. PL/pgSQL学习笔记之二

    39.1.1 使用 PL/pgSQL的好处 SQL是 PostgreSQL和其他大多数关系型数据库作为查询语言而使用的语言.它可移植,并容易学习.但是SQL语句必须被数据库服务器逐条地执行. 这意味着 ...

  2. PL/pgSQL学习笔记之八

    http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 另外一种声明 PL/pgSQL 函数的方法是使用 returns ...

  3. PL/pgSQL学习笔记之七

    http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 如果一个PL/pgSQL函数声明了输出参数,输出参数被赋予$n名 ...

  4. PL/pgSQL学习笔记之五

    http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 39.3. 声明 块中使用的所有的变量必须在块的声明节中进行声明 ...

  5. PL/pgSQL学习笔记之四

    http://www.postgresql.org/docs/9.1/static/plpgsql-structure.html 39.2. PL/pgSQL 的结构 PL/pgSQL是一种块式结构的 ...

  6. PL/pgSQL学习笔记之三

    http://www.postgresql.org/docs/9.1/static/plpgsql-overview.html 39.1.2. Supported Argument and Resul ...

  7. PL/pgSQL学习笔记之一

    开始 资料来源:http://www.postgresql.org/docs/9.1/static/plpgsql-overview.html 39.1 概要: PL/pgSQL是一种可载入的过程语言 ...

  8. PL/pgSQL学习笔记之十一

    http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 39.3.4. Row 类型 name table_name%R ...

  9. PL/pgSQL学习笔记之十

    http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 39.3.3. 类型拷贝 variable%TYPE %TYPE ...

随机推荐

  1. Eclipse c++环境搭建 并加载OpenCV库 2015最新

    C++: 搜索 Eclipse c++ 即可 注意新版的mingw安装器,要安装: 1.mingw-developer-toolkit 2.mingw32-base 3.mingw32-gcc-g++ ...

  2. android 带边框的圆角按钮

    新建buttonstyle.xml 代码如下 <?xml version="1.0" encoding="UTF-8"?> <layer-li ...

  3. Red and Black ---路线问题

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

  4. jsoup 获取指定页面的所有链接(需后续完善)

    java代码如下: import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; impor ...

  5. 【LeetCode】189 - Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  6. 推荐一个网站Stack Overflow

    网站URL:http://stackoverflow.com 我是怎么知道这个网站的呢?其实这个网站非常出名的,相信许多人都知道.如果你不知道,请继续阅读: 一次我在CSDN上面提问,但是想要再问多几 ...

  7. mvc中@RenderSection()研究

    一.@RenderSection定义 HelperResult RenderSection(string name) 但是当如果使用了_Layout.cshtml做母版页的页没有实现Section的话 ...

  8. 整理string类常见方法的使用说明

    整理String类的Length().charAt().getChars().replace().toUpperCase().toLowerCase().trim().toCharArray()使用说 ...

  9. jquery ajax跨域的完美解决方法(jsonp方式)

    ajax跨域请求的问题,JQuery对于Ajax的跨域请求有两类解决方案,不过都是只支持get方式,接下来为大家详细介绍下客户端JQuery.ajax的调用代码     今天在项目中需要做远程数据加载 ...

  10. es6转码器-babel

    babel 基本使用 安装转码规则 # ES2015转码规则 $ npm install --save-dev babel-preset-es2015 # react转码规则 $ npm instal ...