- PostgreSQL 11 Server Side Programming Quick Start Guide
- Luca Ferrari
- 351字
- 2021-06-10 19:23:05
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
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!
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!
- 構(gòu)建高質(zhì)量的C#代碼
- 一本書玩轉(zhuǎn)數(shù)據(jù)分析(雙色圖解版)
- Learning Social Media Analytics with R
- 精通Excel VBA
- Windows內(nèi)核原理與實現(xiàn)
- 完全掌握AutoCAD 2008中文版:綜合篇
- Linux服務與安全管理
- 西門子變頻器技術入門及實踐
- SMS 2003部署與操作深入指南
- 重估:人工智能與賦能社會
- FreeCAD [How-to]
- 大話數(shù)據(jù)科學:大數(shù)據(jù)與機器學習實戰(zhàn)(基于R語言)
- 7天精通Photoshop CS5平面視覺設計
- Flash CS3動畫制作融會貫通
- Internet of Things with Raspberry Pi 3