3

有什么方法可以将 PostgreSQL 中的变量表示为字符串?

例子:

\set table_name countries
SELECT 'SELECT * FROM ' || CAST( :table_name, 'text' ) AS specificQuery;

导致此错误:

ERROR:  syntax error at or near ","
LINE 1: SELECT 'SELECT * FROM ' || CAST( countries, 'text' ) AS specificQuery;

从上面的行示例很明显,它不会将“国家”转换为字符串,而是表示为列/表的名称。

我该如何转换它?

4

2 回答 2

8

你在寻找这样的东西吗?:

SELECT * FROM :"table_name";

http://www.postgresql.org/docs/current/interactive/app-psql.html#APP-PSQL-VARIABLES

于 2012-05-13T21:43:11.680 回答
2

像这样的东西:

SELECT 'SELECT * FROM ' || countries::text AS specificQuery;
于 2012-05-13T21:14:38.983 回答