我想要做的就是能够使用do
块来设置一些变量,然后使用这些变量将查询返回给STDOUT
。
DO $$
DECLARE book_name TEXT;
DECLARE book_slug TEXT;
BEGIN
book_name := 'Ise Monogatari';
book_slug := 'ise';
SELECT bk.id,
bk.created_at,
bk.updated_at,
bk.title,
bk.japanese_title,
bk.content,
bk.description,
'public/cjp/' || book_slug || '/images/cover.png' cover_image_path,
'public/cjp/' || book_slug || '/images/title.png' title_image_path,
'public/cjp/' || book_slug || '/images/thumb_left.png' thumbnail_path,
'public/cjp/' || book_slug || '/images/background.png' background_image_path,
bk.about,
bk.published
FROM books bk
WHERE bk.title = book_name;
END $$;
我收到以下错误:
[42601] ERROR: query has no destination for result data
Hint: If you want to discard the results of a SELECT, use PERFORM instead.
Where: PL/pgSQL function inline_code_block line 8 at SQL statement
感谢任何帮助。
答案 0 :(得分:1)
您无法从DO命令返回任何结果。 The documentation says(强调补充):
代码块被视为没有参数的函数体,返回void。
您可以使用公用表表达式:
WITH args(book_name, book_slug) AS (
VALUES ('Ise Monogatari', 'ise')
)
SELECT bk.id,
bk.created_at,
bk.updated_at,
bk.title,
bk.japanese_title,
bk.content,
bk.description,
'public/cjp/' || book_slug || '/images/cover.png' cover_image_path,
'public/cjp/' || book_slug || '/images/title.png' title_image_path,
'public/cjp/' || book_slug || '/images/thumb_left.png' thumbnail_path,
'public/cjp/' || book_slug || '/images/background.png' background_image_path,
bk.about,
bk.published
FROM books bk
CROSS JOIN args
WHERE bk.title = book_name;