【问题标题】:select Postgres enumeration only if it exists仅当 Postgres 枚举存在时才选择它
【发布时间】:2016-05-04 16:32:16
【问题描述】:

This question 告诉如何检查 Postgres 中是否存在类型。我试着这样使用它:

select
case when exists (select 1 from pg_type where typname = 'my_type')
then unnest(enum_range(NULL::my_type))
else null
end

不幸的是,Postgres 并不懒惰,所以当类型不存在时,这会给我一个类型错误。我该怎么做?

【问题讨论】:

标签: postgresql


【解决方案1】:

如果你只是想枚举一个类型的值,你可以从 pg_enum 得到没有类型转换:

 SELECT
   T.typname,
   E.enumlabel,
   E.enumsortorder
 FROM
   pg_enum E
   INNER JOIN pg_type T ON (E.enumtypid = T.oid)
 WHERE
   T.typname = 'my_type'
 ;

【讨论】:

    猜你喜欢
    • 2015-01-16
    • 1970-01-01
    • 2015-05-06
    • 2021-04-02
    • 2019-11-22
    • 2021-11-19
    • 2013-06-14
    • 2012-06-06
    • 2013-03-10
    相关资源
    最近更新 更多