簡體   English   中英

具有多列輸出的postgresql函數

[英]postgresql function with multiple columns output

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;如果空白或為空,則希望列prec返回或在輸出中顯示值為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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM