to_regclass()

一个将关系名称转换为 OID 的函数

to_regclass() 是一个系统函数,用于将关系(relation)的名称转换为其 OID

to_regclass() 函数是在 PostgreSQL 9.4 中添加的。

用法

to_regclass ( text ) → regclass

to_regclass() 函数等同于使用 regclass 对象标识符类型 进行转换,但如果找不到匹配的关系,它会返回 NULL 而不是引发 ERROR

在评估提供的关系名称时,会考虑当前的搜索路径。

变更历史

示例

to_regclass() 的基本用法示例

postgres=# SELECT to_regclass('foo'), to_regclass('foo')::oid;
 to_regclass | to_regclass 
-------------+-------------
 foo         |       16392
(1 row)

如果指定的关系不存在或未找到,则返回 NULL

postgres=# SELECT to_regclass('bar') IS NULL;
 ?column? 
----------
 t
(1 row)

如果指定的关系在当前搜索路径中不可见,则必须对其进行模式限定。

postgres=# SET search_path TO '';
SET

postgres=# SELECT to_regclass('foo'), to_regclass('public.foo');
 to_regclass | to_regclass 
-------------+-------------
             | public.foo
(1 row)

分类

系统函数

另请参阅

regclass, to_regnamespace()

反馈

提交关于“to_regclass()”的任何评论、建议或更正,请 在此处