在 postgresql 的 WITH 查询中使用 EXECUTE

Using EXECUTE in WITH query in postgresql

我正在使用执行查询从 table 中提取数据。 现在我想将这些数据存储在某个变量中以用于其他操作,我不能使用 temp table 所以我必须使用 WITH 查询。

--Something like this:

with ttable (col1, col2) as (execute 'select tcol1, tcol2 from tab_sample')
insert into tab_sam2 select col1,col2 from ttable;

现在这让我在执行时出错,说执行时或接近执行时出现语法错误。

我该怎么做。 此外,是否有任何替代方法可以在不使用数组或临时 table 的情况下将来自 table 的多个数据存储在 procedure/function 中?

EXECUTE是PL/pgSQL语句,不能混入SQL.

你为什么不这样做:

EXECUTE E'WITH ttable (col1, col2) as (\n'
         '      SELECT tcol1, tcol2 FROM tab_sample\n'
         '   )\n'
         'INSERT INTO tab_sam2\n'
         '   SELECT col1,col2 FROM ttable';