有没有办法将 EXECUTE query_str 返回的行存储在临时 table 中或用作子查询?
Is there a way to store rows returned by EXECUTE query_str in a temp table or use as a subquery?
考虑这样的功能:
drop function if exists some_func;
create function some_func(table_name text, field_name text)
returns void as $$
declare query_str text;
begin
query_str := format(
'update %1$I' ||
'set %2$I = 1', ||
'some_other_value = null' ||
'returning id, %1$s, %2$I',
table_name,
field_name
);
query_str = case when field_name = 'some value'
-- offtopic: i'd also appreciate a better way todo this :)
then regexp_replace(query_str, ',\s*some_other_value = null', '')
else query_str
end;
execute query_str;
end;
$$ language plpgsql;
我需要一种方法来将此更新查询 execute query_str;
返回的行保存在临时 table 中或直接在另一个更新查询中使用。
类似于:
create temp table my_tmp_tbl on commit drop as execute query_str;
-- or
with update_sql as execute query_str create temp table my_tmp_table as select * from update_sql;
-- or
update some_other_table
set some_field = update_query.field_name
from execute squery_str as update_query
where some_other_table.object_id = update_query.id;
是否可以按照这些思路做一些事情?
PL/pgSQL 没有任何直接工具可以完成此任务。
我觉得有3种可能:
- 使用
FOR EXECUTE
周期:
DECLARE r record;
BEGIN
FOR r IN EXECUTE 'UPDATE ... RETURNING id, c2, c3'
LOOP
INSERT INTO tmp_tab VALUES(r.id, r.c2, r.c3);
END LOOP;
END;
- 使用
RETURN QUERY EXECUTE
和 INSERT SELECT
:
CREATE OR REPLACE FUNCTION fx()
RETURNS TABLE(id int, c2 int, c3 int)
AS $$
BEGIN
RETURN QUERY EXECUTE 'UPDATE ... RETURNING id, c2, c3';
END;
$$ LANGUAGE plpgsql
CREATE OR REPLACE FUNCTION outer_fx()
RETURNS void AS $$
BEGIN
INSERT INTO tmp_tab SELECT * FROM fx();
END;
$$ LANGUAGE plpgsql;
- 可能最好的方法是直接在动态 SQL:
中将数据存储在 tmp table 中
DECLARE r record;
BEGIN
EXECUTE 'WITH q1 AS (UPDATE ... RETURNING id, c2, c3)'
'INSERT INTO tmp_tab SELECT * FROM q1';
...
考虑这样的功能:
drop function if exists some_func;
create function some_func(table_name text, field_name text)
returns void as $$
declare query_str text;
begin
query_str := format(
'update %1$I' ||
'set %2$I = 1', ||
'some_other_value = null' ||
'returning id, %1$s, %2$I',
table_name,
field_name
);
query_str = case when field_name = 'some value'
-- offtopic: i'd also appreciate a better way todo this :)
then regexp_replace(query_str, ',\s*some_other_value = null', '')
else query_str
end;
execute query_str;
end;
$$ language plpgsql;
我需要一种方法来将此更新查询 execute query_str;
返回的行保存在临时 table 中或直接在另一个更新查询中使用。
类似于:
create temp table my_tmp_tbl on commit drop as execute query_str;
-- or
with update_sql as execute query_str create temp table my_tmp_table as select * from update_sql;
-- or
update some_other_table
set some_field = update_query.field_name
from execute squery_str as update_query
where some_other_table.object_id = update_query.id;
是否可以按照这些思路做一些事情?
PL/pgSQL 没有任何直接工具可以完成此任务。
我觉得有3种可能:
- 使用
FOR EXECUTE
周期:
DECLARE r record;
BEGIN
FOR r IN EXECUTE 'UPDATE ... RETURNING id, c2, c3'
LOOP
INSERT INTO tmp_tab VALUES(r.id, r.c2, r.c3);
END LOOP;
END;
- 使用
RETURN QUERY EXECUTE
和INSERT SELECT
:
CREATE OR REPLACE FUNCTION fx()
RETURNS TABLE(id int, c2 int, c3 int)
AS $$
BEGIN
RETURN QUERY EXECUTE 'UPDATE ... RETURNING id, c2, c3';
END;
$$ LANGUAGE plpgsql
CREATE OR REPLACE FUNCTION outer_fx()
RETURNS void AS $$
BEGIN
INSERT INTO tmp_tab SELECT * FROM fx();
END;
$$ LANGUAGE plpgsql;
- 可能最好的方法是直接在动态 SQL: 中将数据存储在 tmp table 中
DECLARE r record;
BEGIN
EXECUTE 'WITH q1 AS (UPDATE ... RETURNING id, c2, c3)'
'INSERT INTO tmp_tab SELECT * FROM q1';
...