我想做的就是能够使用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
任何帮助表示赞赏。
您无法从 DO 命令返回任何结果。文档说 https://www.postgresql.org/docs/current/static/sql-do.html(强调):
代码块被视为没有参数的函数体,返回无效。
您可以使用公用表表达式:
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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)