There are several ways to determine the effect of a command. The first method is to use the GET
DIAGNOSTICS command, which has the form:
GET [ CURRENT ] DIAGNOSTICS variable { = | := } item [ , ... ];
This command allows retrieval of system status indicators. Each item is a key word identifying a
status value to be assigned to the specified variable (which should be of the right data type to receive
it). The currently available status items are ROW_COUNT, the number of rows processed by the last
SQL command sent to the SQL engine, and RESULT_OID, the OID of the last row inserted by the most
recent SQL command. Note that RESULT_OID is only useful after an INSERT command into a table
containing OIDs. Colon-equal (:=) can be used instead of SQL-standard = for GET DIAGNOSTICS.
An example:
GET DIAGNOSTICS integer_var = ROW_COUNT;
The second method to determine the effects of a command is to check the special variable named
FOUND, which is of type boolean. FOUND starts out false within each PL/pgSQL function call. It is
set by each of the following types of statements:

• A SELECT INTO statement sets FOUND true if a row is assigned, false if no row is returned.
• A PERFORM statement sets FOUND true if it produces (and discards) one or more rows, false if no
row is produced.
• UPDATE, INSERT, and DELETE statements set FOUND true if at least one row is affected, false if no
row is affected.
• A FETCH statement sets FOUND true if it returns a row, false if no row is returned.
• A MOVE statement sets FOUND true if it successfully repositions the cursor, false otherwise.
• A FOR or FOREACH statement sets FOUND true if it iterates one or more times, else false. FOUND is
set this way when the loop exits; inside the execution of the loop, FOUND is not modified by the
loop statement, although it might be changed by the execution of other statements within the loop
body.
• RETURN QUERY and RETURN QUERY EXECUTE statements set FOUND true if the query returns at
least one row, false if no row is returned.
Other PL/pgSQL statements do not change the state of FOUND. Note in particular that EXECUTE
changes the output of GET DIAGNOSTICS, but does not change FOUND.
FOUND is a local variable within each PL/pgSQL function; any changes to it affect only the current
function.

Executing a Query with a Single-row Result

The result of a SQL command yielding a single row (possibly of multiple columns) can be assigned to
a record variable, row-type variable, or list of scalar variables. This is done by writing the base SQL
command and adding an INTO clause. For example,
SELECT select_expressions INTO [STRICT] target FROM ...;
INSERT ... RETURNING expressions INTO [STRICT] target;
UPDATE ... RETURNING expressions INTO [STRICT] target;
DELETE ... RETURNING expressions INTO [STRICT] target;
where target can be a record variable, a row variable, or a comma-separated list of simple variables
and record/row fields. PL/pgSQL variables will be substituted into the rest of the query, and the plan
is cached, just as described above for commands that do not return rows. This works for SELECT,
INSERT/UPDATE/DELETE with RETURNING, and utility commands that return row-set results (such as
EXPLAIN). Except for the INTO clause, the SQL command is the same as it would be written outside
PL/pgSQL.

Tip: Note that this interpretation of SELECT with INTO is quite different from PostgreSQL’s regular
SELECT INTO command, wherein the INTO target is a newly created table. If you want to create
a table from a SELECT result inside a PL/pgSQL function, use the syntax CREATE TABLE ... AS
SELECT.

If a row or a variable list is used as target, the query’s result columns must exactly match the structure
of the target as to number and data types, or else a run-time error occurs. When a record variable is
the target, it automatically configures itself to the row type of the query result columns.
The INTO clause can appear almost anywhere in the SQL command. Customarily it is written either
just before or just after the list of select_expressions in a SELECT command, or at the end of the
command for other command types. It is recommended that you follow this convention in case the
PL/pgSQL parser becomes stricter in future versions.
If STRICT is not specified in the INTO clause, then target will be set to the first row returned by the
query, or to nulls if the query returned no rows. (Note that “the first row” is not well-defined unless
you’ve used ORDER BY.) Any result rows after the first row are discarded. You can check the special
FOUND variable (see Section 40.5.5) to determine whether a row was returned:
SELECT * INTO myrec FROM emp WHERE empname = myname;
IF NOT FOUND THEN
RAISE EXCEPTION ’employee % not found’, myname;
END IF;

If the STRICT option is specified, the query must return exactly one row or a run-time error will be
reported, either NO_DATA_FOUND (no rows) or TOO_MANY_ROWS (more than one row). You can use
an exception block if you wish to catch the error, for example:
BEGIN
SELECT * INTO STRICT myrec FROM emp WHERE empname = myname;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE EXCEPTION ’employee % not found’, myname;
WHEN TOO_MANY_ROWS THEN
RAISE EXCEPTION ’employee % not unique’, myname;
END;
Successful execution of a command with STRICT always sets FOUND to true.
For INSERT/UPDATE/DELETE with RETURNING, PL/pgSQL reports an error for more than one returned
row, even when STRICT is not specified. This is because there is no option such as ORDER BY
with which to determine which affected row should be returned.
If print_strict_params is enabled for the function, then when an error is thrown because the
requirements of STRICT are not met, the DETAIL part of the error message will include information
about the parameters passed to the query. You can change the print_strict_params setting for all
functions by setting plpgsql.print_strict_params, though only subsequent function compilations
will be affected. You can also enable it on a per-function basis by using a compiler option, for
example:

CREATE FUNCTION get_userid(username text) RETURNS int
AS $$
#print_strict_params on
DECLARE
userid int;
BEGIN

SELECT users.userid INTO STRICT userid
FROM users WHERE users.username = get_userid.username;
RETURN userid;
END
$$ LANGUAGE plpgsql;
On failure, this function might produce an error message such as
ERROR: query returned no rows
DETAIL: parameters: $1 = ’nosuchuser’
CONTEXT: PL/pgSQL function get_userid(text) line 6 at SQL statement
Note: The STRICT option matches the behavior of Oracle PL/SQL’s SELECT INTO and related
statements.
To handle cases where you need to process multiple result rows from a SQL query, see Section 40.6.4.

PostgreSQL Obtaining the Result Status的更多相关文章

  1. postgreSQL PL/SQL编程学习笔记(一)

    1.Structure of PL/pgSQL The structure of PL/pgSQL is like below: [ <<label>> ] [ DECLARE ...

  2. PostgreSQL windows service启动失败

    from: http://stackoverflow.com/questions/1251233/unable-to-run-postgresql-as-windows-servicepg_ctl - ...

  3. postgresql 主从复制并切换

    1 环境 192.168.19.145 Kylin 3.3 mysqlhq  9.5.2  psql_master192.168.19.227 Kylin 3.3 mysql3    9.5.2  p ...

  4. postgresql windows 服务启动失败

    1命令行 启动服务 pg_ctl -D "C:\Program Files\PostgreSQL\9.1\data" start 2 查看状态 pg_ctl -D "C: ...

  5. 对ansible不支持service模块的status命令进行修正

    原生的ansible不支持service.status,在Google之后,发现有人提交了一个patch,可以支持status选项.见https://github.com/ritzk/ansible- ...

  6. Ubuntu20.04 PostgreSQL 14 安装配置记录

    PostgreSQL 名称来源 It was originally named POSTGRES, referring to its origins as a successor to the Ing ...

  7. python tornado框架实现CRUD

    1.本例采用postgresql数据库,创建数据表 user_tbl ),signup_date date); 2.webapi接口 (1)tornado框架配置 t_tornado.py #-*- ...

  8. Django(一)

    Django 一.什么是web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演 ...

  9. restful设计规范

    什么是restful? REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” REST从资源的角度类 ...

随机推荐

  1. Bootstrap页面布局15 - BS带下拉菜单的按钮

    带下拉菜单的按钮 <div class='btn-toolbar'> <div class='btn-group'> <a href='javascript:;' cla ...

  2. NSQ:分布式的实时消息平台

    NSQ是一个基于Go语言的分布式实时消息平台,它基于MIT开源协议发布,代码托管在GitHub,其当前最新版本是0.3.1版.NSQ可用于大规模系统中的实时消息服务,并且每天能够处理数亿级别的消息,其 ...

  3. PHP MongoDB 扩展安装配置

    近日对MongoDB比较感兴趣,在linux下部署了一套LAMP,想把MongoDB加进来,下面进入正题: 1.确保安装好LAMP环境,假设php安装目录:/usr/local/php5 2.下载ht ...

  4. php 分词 —— PHPAnalysis无组件分词系统

    分词,顾名思义就是把词语分开,从哪里分开?当然是一大堆词语里了,一大堆词语是什么?是废话或者名言.这在数据库搜索时非常有用. 官方网站 http://www.phpbone.com/phpanalys ...

  5. git-svn

    sudo apt-get install git-svn svn作为一个优秀源码版本的管理工具,可以适合绝大多数项目.但是因为它的采用中心化管理,不可避免的存在本地代码的备份和版本管理问题.也就是说对 ...

  6. 通过IP获得IP所在地的三个接口

    http://ip.qq.com/cgi-bin/searchip?searchip1=180.168.144.211 http://ip.taobao.com/service/getIpInfo.p ...

  7. 实验一 Java开发环境的熟悉-刘蔚然

    一.实验内容 1. 使用JDK编译.运行简单的Java程序 2. 使用Eclipse 编辑.编译.运行.调试Java程序 要求: 完成实验.撰写实验报告 统计PSP时间 二.实验过程 使用JDK编译. ...

  8. CENTOS安装vnc

    先直接进入命令模式,如果是服务器则可以使用putty连接进入命令行模式. 现在知道的centos下的vnc是:tigervnc,由于是服务端所以我们只安装tigervnc-server即可: yum ...

  9. 【C++Q】

    //c_str const char* str2Cchar(string s){ //const char* ss = s.c_str(); //出错,因为s会被析构,ss指向垃圾内容 ]; strc ...

  10. ubuntu12.04进入单用户模式

    更改ubuntu配置文件出错了,只能进入单用户模式改回来. 转载至: http://blog.csdn.net/lfyaa/article/details/9899497 1.重启ubuntu,VMw ...