Postgresql 中文操作指南

PREPARE

PREPARE — 准备一个待执行的语句

PREPARE — prepare a statement for execution

Synopsis

PREPARE prepared_name FROM string

Description

PREPARE 为作为动态指定的字符串准备一个待执行的语句。这不同于直接 SQL 语句 PREPARE ,也可以在嵌入式程序中使用。 EXECUTE 命令用于执行任一类型的已准备语句。

PREPARE prepares a statement dynamically specified as a string for execution. This is different from the direct SQL statement PREPARE, which can also be used in embedded programs. The EXECUTE command is used to execute either kind of prepared statement.

Parameters

  • prepared_name #

    • An identifier for the prepared query.

  • string #

    • A literal string or a host variable containing a preparable SQL statement, one of SELECT, INSERT, UPDATE, or DELETE. Use question marks (?) for parameter values to be supplied at execution.

Notes

在典型用法中, string 是对包含动态构造的 SQL 语句的字符串的主机变量引用。文字字符串的情况没有什么用,你不仅可以通过编写直接的 SQL PREPARE 语句来解决。

In typical usage, the string is a host variable reference to a string containing a dynamically-constructed SQL statement. The case of a literal string is not very useful; you might as well just write a direct SQL PREPARE statement.

如果你确实使用文字字符串,请记住你可能希望包含在 SQL 语句中的任何双引号必须写成八进制转义字符 ( \042 ),而不是通常的 C 惯用法 \" 。这是因为字符串位于 EXEC SQL 部分中,因此 ECPG 词法分析器根据 SQL 规则而不是 C 规则对其进行解析。任何嵌入的反斜杠都会根据 C 规则得到处理;但 \" 会引起立即的语法错误,因为它被视为结束的文字。

If you do use a literal string, keep in mind that any double quotes you might wish to include in the SQL statement must be written as octal escapes (\042) not the usual C idiom \". This is because the string is inside an EXEC SQL section, so the ECPG lexer parses it according to SQL rules not C rules. Any embedded backslashes will later be handled according to C rules; but \" causes an immediate syntax error because it is seen as ending the literal.

Examples

char *stmt = "SELECT * FROM test1 WHERE a = ? AND b = ?";

EXEC SQL ALLOCATE DESCRIPTOR outdesc;
EXEC SQL PREPARE foo FROM :stmt;

EXEC SQL EXECUTE foo USING SQL DESCRIPTOR indesc INTO SQL DESCRIPTOR outdesc;

Compatibility

PREPARE 在 SQL 标准中指定。

PREPARE is specified in the SQL standard.

See Also