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

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
主站蜘蛛池模板: 潮安县| 广平县| 星座| 建宁县| 敖汉旗| 平乡县| 威宁| 江津市| 浦城县| 九龙城区| 新野县| 峨眉山市| 浙江省| 乡城县| 汶上县| 沧州市| 德兴市| 攀枝花市| 汾西县| 衡水市| 富阳市| 庐江县| 蒲江县| 景洪市| 澳门| 广水市| 绍兴县| 榕江县| 河间市| 崇礼县| 海丰县| 南投县| 松滋市| 富顺县| 汝南县| 宝丰县| 宣恩县| 邹平县| 孝昌县| 顺昌县| 元江|