Postgresql 中文操作指南
EXECUTE IMMEDIATE
EXECUTE IMMEDIATE — 动态准备并执行一条语句
EXECUTE IMMEDIATE — dynamically prepare and execute a statement
Description
EXECUTE IMMEDIATE 立即准备并执行一条动态指定的 SQL 语句,而不检索结果行。
EXECUTE IMMEDIATE immediately prepares and executes a dynamically specified SQL statement, without retrieving result rows.
Parameters
-
string #
-
A literal string or a host variable containing the SQL statement to be executed.
-
Notes
在典型用法中, string 对一个包含动态构造的 SQL 语句的字符串的主机变量的引用。文本字符串的情况并不是很有用;你也可以直接编写 SQL 语句,而不需要使用 EXECUTE IMMEDIATE 进行额外的键入。
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 the SQL statement directly, without the extra typing of EXECUTE IMMEDIATE.
如果你确实使用文字字符串,请记住你可能希望包含在 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
下面是一个使用 EXECUTE IMMEDIATE 和名为 command 的主机变量执行 INSERT 语句的示例:
Here is an example that executes an INSERT statement using EXECUTE IMMEDIATE and a host variable named command:
sprintf(command, "INSERT INTO test (name, amount, letter) VALUES ('db: ''r1''', 1, 'f')");
EXEC SQL EXECUTE IMMEDIATE :command;