将执行结果保存到 table
Save execute results into a table
下面是我正在尝试的一个简化的 postgres 存储过程 运行:
create or replace procedure my_schema.tst(suffix varchar)
as $$
begin
execute(' select *
into my_schema.MyTable_'||suffix||'
From my_schema.MyTable
');
end;
$$
language plpgsql;
当我尝试 运行 使用类似的东西时:
call my_schema.tst('test');
我收到此错误 无效操作:SELECT 的 EXECUTE ... 不支持 INTO;
是否可以执行创建新 table 的动态查询?我见过这样的例子:
Execute('... some query ...') into Table;
但对于我的用例,我需要将生成的 tablename 作为变量传递。
在 PostgreSQL 中你可以使用 INSERT INTO tname SELECT...
create or replace procedure my_schema.tst(suffix varchar)
as $$
begin
execute ' INSERT INTO my_schema.MyTable_'||suffix||' SELECT *
FROM my_schema.MyTable
';
end;
$$
language plpgsql;
或使用CREATE TABLE tname AS SELECT...,
:
create or replace procedure my_schema.tst(suffix varchar)
as $$
begin
execute ' CREATE TABLE my_schema.MyTable_'||suffix||' as SELECT *
FROM my_schema.MyTable
';
end;
$$
language plpgsql;
下面是我正在尝试的一个简化的 postgres 存储过程 运行:
create or replace procedure my_schema.tst(suffix varchar)
as $$
begin
execute(' select *
into my_schema.MyTable_'||suffix||'
From my_schema.MyTable
');
end;
$$
language plpgsql;
当我尝试 运行 使用类似的东西时:
call my_schema.tst('test');
我收到此错误 无效操作:SELECT 的 EXECUTE ... 不支持 INTO;
是否可以执行创建新 table 的动态查询?我见过这样的例子:
Execute('... some query ...') into Table;
但对于我的用例,我需要将生成的 tablename 作为变量传递。
在 PostgreSQL 中你可以使用 INSERT INTO tname SELECT...
create or replace procedure my_schema.tst(suffix varchar)
as $$
begin
execute ' INSERT INTO my_schema.MyTable_'||suffix||' SELECT *
FROM my_schema.MyTable
';
end;
$$
language plpgsql;
或使用CREATE TABLE tname AS SELECT...,
:
create or replace procedure my_schema.tst(suffix varchar)
as $$
begin
execute ' CREATE TABLE my_schema.MyTable_'||suffix||' as SELECT *
FROM my_schema.MyTable
';
end;
$$
language plpgsql;