pgsql 想把select 出来的指赋值给一个变量,并使用这个变量分别该怎么做?

Ocean
  • 1.6k

`

postgres=# select oid from pg_class  where relname=:'tablename';
  oid
-------
 24678
(1 row)
以下是我失败的几个尝试:

postgres=# select oid into partition_table_id from pg_class where relname=:'tablename';
ERROR: There is no hash distributable column

postgres=# select oid from pg_class where relname=:'tablename' into partition_table_id;
ERROR: syntax error at or near "into"
LINE 1: ...t oid from pg_class where relname='table_level_1' into parti...

`

回复
阅读 2.8k
2 个回答
DECLARE variable_col int := (SELECT oid FROM pg_class WHERE relname='tablename');

SELECT * FROM pg_class WHERE oid=variable_col;

手头没 pg 库,没执行,大概意思理解一下。

P.S. variable_col 是变量名,我瞎起的;int 记得改成你那个 oid 的数据类型。

晓_mini
  • 5
新手上路,请多包涵
postgres=# select oid as var from pg_class where relname='pg_toast_16384' \gset

postgres=# select oid,relname as var from pg_class where oid=:var;
  oid  |      var
-------+----------------
 16387 | pg_toast_16384
(1 row)

postgres=# select :'var';
 ?column?
----------
 16387
(1 row)

postgres=# select :'var'||'123';
 ?column?
----------
 16387123
(1 row)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏