在Postgres中使用数组数据类型时,我遇到了需要提取维度和数据类型的情况.
dellstore_clear=# \d+ sal_emp
Table "public.sal_emp"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
----------------+-----------+-----------+----------+---------+----------+-------------+--------------+-------------
name | text | | | | extended | | |
pay_by_quarter | integer[] | | | | extended | | |
schedule | text[] | | | | extended | | |
Access method: heap
dellstore_clear=#
可以使用以下命令检索列的维度
dellstore_clear=# select array_ndims(pay_by_quarter) as pay_by_quarter_dim, array_ndims(schedule) as schedule_dim from sal_emp limit 1 ;
pay_by_quarter_dim | schedule_dim
--------------------+--------------
1 | 2
(1 row)
dellstore_clear=#
我试图在从INFORMATION_SCHEMA.COLUMNS
中检索信息时将这两者结合起来,如下所示,
select column_name,
case
when data_type='ARRAY' then udt_name::regtype::text
else data_type
end as column_type,
case
when data_type='ARRAY' then array_ndims(column_name)
else 'NA'
end as array_dimension
from INFORMATION_SCHEMA.COLUMNS where TABLE_CATALOG LIKE 'dellstore_clear' and TABLE_SCHEMA like 'public' and TABLE_NAME like 'sal_emp' order by ordinal_position;
但是,这给出了如下错误,
ERROR: function array_ndims(information_schema.sql_identifier) does not exist
LINE 7: when data_type='ARRAY' then array_ndims(column_name)
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
dellstore_clear=#
dellstore_clear=#
如何将检索到的列名称值传递给同一查询中的后续函数?