postgresql:是否可以 运行 一个 returns 行的操作而不创建函数?
postgresql: is it possible to run an operation that returns rows without creating a function?
我有一些sql结构如下:
DO $$
DECLARE
foo text := 'thisisfoo';
myId bigint;
BEGIN
myId = (select id from blah.things);
insert into bar.widgets(...)
values (myId, foo, ...);
select * from bar.widgets;
END $$ language plpgsql;
当我 运行 这个时,我得到:
ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function inline_code_block line 9 at SQL statement
SQL state: 42601
我尝试过的事情:
- 将查询更改为 insert into ... returning *;
和 return select * from bar.widgets
。两者都会产生错误,看起来创建一个 bar.widget return 类型的函数是唯一的出路。
问题:有没有办法让非函数使用变量和 return table 行? 原因是我不想将其作为函数添加到架构中。它只会被使用几次,我不想弄乱架构。
您可以 运行 select
在 do
块之外:
...
END $$ language plpgsql;
select * from bar.widgets;
或者甚至在没有 do
块的情况下重写它:
insert into bar.widgets(...)
values ((select id from blah.things), 'thisisfoo', ...)
returning *;
我有一些sql结构如下:
DO $$
DECLARE
foo text := 'thisisfoo';
myId bigint;
BEGIN
myId = (select id from blah.things);
insert into bar.widgets(...)
values (myId, foo, ...);
select * from bar.widgets;
END $$ language plpgsql;
当我 运行 这个时,我得到:
ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function inline_code_block line 9 at SQL statement
SQL state: 42601
我尝试过的事情:
- 将查询更改为 insert into ... returning *;
和 return select * from bar.widgets
。两者都会产生错误,看起来创建一个 bar.widget return 类型的函数是唯一的出路。
问题:有没有办法让非函数使用变量和 return table 行? 原因是我不想将其作为函数添加到架构中。它只会被使用几次,我不想弄乱架构。
您可以 运行 select
在 do
块之外:
...
END $$ language plpgsql;
select * from bar.widgets;
或者甚至在没有 do
块的情况下重写它:
insert into bar.widgets(...)
values ((select id from blah.things), 'thisisfoo', ...)
returning *;