[英]PL/pgSQL INSERT INTO table
I have a plpgsql function that creates an execute statement and I want it to execute it into a table. 我有一个创建执行语句的plpgsql函数,我希望它将其执行到表中。 The standard
EXECUTE ... INTO table_name
didn't work, so I was looking for an alternative. 标准的
EXECUTE ... INTO table_name
无效,因此我正在寻找替代方法。 Basically, the select statement returns three columns which I want to save to a table. 基本上,select语句返回三列,我要保存到表中。
Here's some sample code of the execute: 这是执行的一些示例代码:
query = 'SELECT name, work, phone FROM info WHERE name = '
|| quote_literal(inName) || ' ORDER BY phone';
Ideally, if I was just running the query myself I would just put a SELECT INTO tablename, but that didn't work with the execute. 理想情况下,如果我只是自己运行查询,则只需放置一个SELECT INTO表名,但这对execute无效。
Any ideas? 有任何想法吗?
Use CREATE TABLE AS
for that: 为此使用
CREATE TABLE AS
:
EXECUTE 'CREATE TABLE foo AS
SELECT name, work, phone
FROM info WHERE name = ' || quote_literal(in_name) || ' ORDER BY phone';
SELECT INTO
is discouraged for that purpose: 为此不建议使用
SELECT INTO
:
Combine two tables into a new one so that select rows from the other one are ignored 将两个表合并为一个新表,以便忽略另一个表中的选择行
SELECT / EXECUTE .. INTO ..
is meant for single rows, not for whole tables in plpgsql. SELECT / EXECUTE .. INTO ..
是指单行 ,而不是plpgsql中的整个表。
And the assignment operator in plpgsql is :=
, not =
. 并且plpgsql中的赋值运算符是
:=
,而不是=
。
And a single quote was missing. 并且缺少单引号。
And don't use unquoted mixed case identifiers in Postgres. 并且不要在Postgres中使用未加引号的大小写混合标识符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.