具有多列输出的postgresql函数

时间:2018-08-28 11:40:37

标签: sql postgresql

SELECT column_name as name,
       data_type,
      concat(character_maximum_length,numeric_precision,datetime_precision) as length,
      concat(numeric_precision,datetime_precision) as prec,
      is_nullable as isnullable,
      COALESCE(numeric_scale,'0') as scale,
      null as definition,
      null as iscomputed,
      null as is_sparse,
      column_default as "default",
      null as is_persisted
FROM information_schema.columns
WHERE (table_schema, table_name) = ('test', 'testtable')
ORDER BY ordinal_position;

当前输出:

         name         |          data_type          | length | prec | isnullable | scale | definition | iscomputed | is_sparse |      default       | is_persisted
----------------------+-----------------------------+--------+------+------------+-------+------------+------------+-----------+--------------------+--------------
 keytestpostgresql    | uuid                        |        |      | NO         |     0 |            |            |           | uuid_generate_v4() |
 testsmalldatetime    | timestamp without time zone | 0      | 0    | YES        |     0 |            |            |           |                    |
 testdatetime         | timestamp without time zone | 3      | 3    | YES        |     0 |            |            |           |                    |
 testint              | integer                     | 32     | 32   | YES        |     0 |            |            |           |                    |
 testdatenotime       | date                        | 0      | 0    | YES        |     0 |            |            |           |                    |
 testnmemo            | text                        |        |      | YES        |     0 |            |            |           |                    |
 testfloat            | double precision            | 53     | 53   | YES        |     0 |            |            |           |                    |
 testboolean          | boolean                     |        |      | YES        |     0 |            |            |           |                    |
 biginttest           | bigint                      | 64     | 64   | YES        |     0 |            |            |           |                    |
 testtinyint          | smallint                    | 16     | 16   | YES        |     0 |            |            |           |                    |
 testsmallint         | smallint                    | 16     | 16   | YES        |     0 |            |            |           |                    |
 testuniqueidentifier | uuid                        |        |      | YES        |     0 |            |            |           |                    |
 testnumeric          | numeric                     | 5      | 5    | YES        |     2 |            |            |           |                    |
 updoperation         | smallint                    | 16     | 16   | NO         |     0 |            |            |           | 0                  |
 upddate              | timestamp without time zone | 3      | 3    | NO         |     0 |            |            |           | now()              |
(15 rows)

想要列长度以返回或在输出中将值显示为-1(如果空白或为null) 如果空白或为空,希望列prec返回或在输出中将值显示为0

1 个答案:

答案 0 :(得分:0)

请参考以下查询:

SELECT column_name as name,
       data_type,
      coalesce(character_maximum_length, numeric_precision, datetime_precision, -1) as length,
      concat(numeric_precision,datetime_precision) as prec,
      is_nullable as isnullable,
      COALESCE(numeric_scale,'0') as scale,
      null as definition,
      null as iscomputed,
      null as is_sparse,
      column_default as "default",
      null as is_persisted
FROM information_schema.columns
WHERE (table_schema, table_name) = ('test', 'testtable')
ORDER BY ordinal_position;