官术网_书友最值得收藏!

Throwing away a query result set

Any SQL statement that returns values must put its result into one or more variables by using INTO, for example. This means that invoking other functions that return values, or executing statements that return a result set (such as INSERT, UPDATE or DELETE with RETURNING or SELECT) is not possible until the returned values are stored in variables. This means that even a simple block code such as Listing 35 will fail because the value returned by the now() function is implicitly discarded:

testdb=> DO $code$ BEGIN
SELECT now();
END $code$;

ERROR: query has no destination for result data
Listing 35:  A throw-away statement that doesn't work

If we have the need to execute a query and throw away its results, the PERFORM statement must be used. This statement can be thought of an alias for SELECT. It is in fact possible to write the query text in the same way, including variable interpolation, just by substituting the SELECT keyword with PERFORM. As an example, Listing 36 shows a couple of queries that throw away the result without leading to the abortion of the execution:

testdb=> DO $code$
DECLARE
file_type text := 'png';
BEGIN
-- ok
PERFORM now();
PERFORM -- SELECT
f_size, f_name
FROM files
WHERE f_type = file_type -- interpolation
ORDER BY f_size DESC;
RAISE INFO 'I've survived!';
END $code$;

INFO: I've survived!
Listing 36:  Throwing away results with PERFORM

If the query that must throw away results is a CTE, the whole query must be passed in parentheses as argument to PERFORM, without substituting the SELECT keyword in the top-level statement, as shown in Listing 37. However, in this case, the CTE must return a single row from the top-level statement or the execution will be aborted:

testdb=> DO $code$
DECLARE
file_type text := 'png';
BEGIN
PERFORM ( WITH biggest_file_by_type AS (
SELECT f_name FROM files
WHERE f_type = file_type
ORDER BY f_size DESC
LIMIT 1 ) SELECT f_name
FROM biggest_file_by_type );

RAISE INFO 'I've survived!';
END $code$;

INFO: I've survived!
Listing 37:  Throwing away results with PERFORM and CTE
主站蜘蛛池模板: 时尚| 墨玉县| 南平市| 平定县| 固安县| 鹤岗市| 五原县| 正蓝旗| 朔州市| 葫芦岛市| 上杭县| 通榆县| 邹平县| 江门市| 上思县| 罗田县| 保康县| 武宁县| 游戏| 宁河县| 桃园市| 绵竹市| 从江县| 兰考县| 宝坻区| 青岛市| 永善县| 湖口县| 同仁县| 孟津县| 祁连县| 临沭县| 临洮县| 新丰县| 湾仔区| 宁海县| 东平县| 航空| 长白| 莱阳市| 聊城市|