准备Demo表
postgres=# CREATE TABLE tab1 (a INT, b CHAR(16)); postgres=# INSERT INTO tab1 VALUES(93, 'xyz'); postgres=# SELECT * FROM tab1; a | b ----+------------------ 93 | xyz (1 ROW)
postgres=# \SET my_var tab1 postgres=# \echo :my_var tab1 postgres=# SELECT * FROM :my_var; a | b ----+------------------ 93 | xyz (1 ROW)
注:
该命令在PG12版本新增。
例如:
postgres=# SELECT b AS my_var FROM tab1 \gset postgres=# \echo :my_var xyz
以下示例,将时间戳赋值给变量my_var:
postgres=# SELECT CAST(EXTRACT(epoch FROM now()) AS BIGINT) AS my_var \gset postgres=# \echo :my_var 1609133432 postgres=# \echo VIEW:my_var view1609133432
注: