更新时间:2022-12-09 12:37:54
您将需要使用 PL / PgSQL EXECUTE
语句,通过 DO
块或PL / PgSQL函数(创建或替换功能...语言plpgsql
)。 PostgreSQL使用的普通SQL方言仅在程序PL / PgSQL变体中不支持Dynamic SQL。
You will need to use the PL/PgSQL EXECUTE
statement, via a DO
block or PL/PgSQL function (CREATE OR REPLACE FUNCTION ... LANGUAGE plpgsql
). Dynamic SQL is not supported in the ordinary SQL dialect used by PostgreSQL, only in the procedural PL/PgSQL variant.
DO
$$
BEGIN
EXECUTE format('CREATE TABLE %I AS SELECT * FROM backup', 'backup_' || to_char(CURRENT_DATE,'yyyy-mm-dd'));
END;
$$ LANGUAGE plpgsql;
format(...)
函数的%I
和%L
格式说明符分别执行正确的标识符和文字引号。
The format(...)
function's %I
and %L
format-specifiers do proper identifier and literal quoting, respectively.
对于文字,我建议使用 EXECUTE ...使用
而不是 format(。 )
和%L
,但对于表/列之类的标识符,其格式为%I
模式是详细的 quote_ident
调用的一种很好的简洁选择。
For literals I recommend using EXECUTE ... USING
rather than format(...)
with %L
, but for identifiers like table/column names the format %I
pattern is a nice concise alternative to verbose quote_ident
calls.