查詢 SQL (適用 USERSPACE1 表空間下的使用者表)
SELECT
d.name AS tbName,
COALESCE(d.remarks, '') AS tbDesc,
a.name AS columnName,
a.coltype AS columnType,
DECODE(a.nulls, 'Y', '1', '0') AS notNull,
DECODE(a.identity, 'Y', '1', '0') AS auto,
a.longlength AS width,
a.scale AS precision,
COALESCE(a.remarks, '') AS comment,
DECODE(n.unique_colcount, '1', '1', '0') AS unique,
DECODE(n.uniquerule, 'P', '1', '0') AS masterKey,
COALESCE(n.name, '') AS indexName
FROM sysibm.syscolumns a
INNER JOIN sysibm.systables d ON a.tbname = d.name
LEFT JOIN sysibm.sysindexes n
ON n.tbname = d.name
AND SUBSTR(n.colnames, 2) = a.name
WHERE d.type = 'T'
AND d.tbspace = 'USERSPACE1';
欄位說明
| 輸出欄位 | 意義 |
| tbName | 表名稱 |
| tbDesc | 表備註 |
| columnName | 欄位名稱 |
| columnType | 資料型別代碼 |
| notNull | 1=NOT NULL, 0=可空 |
| auto | 1=IDENTITY 自增, 0=否 |
| width | 長度 (長字串/LOB 用) |
| precision | 小數位數 |
| comment | 欄位備註 |
| unique | 1=唯一索引欄位 |
| masterKey | 1=主鍵欄位 |
| indexName | 索引名稱 |
調整建議
- 若需查詢所有表空間,移除
AND d.tbspace = 'USERSPACE1'。
- 可加入
AND d.tabschema = CURRENT SCHEMA 限定當前 schema。
- 索引欄位對應邏輯
SUBSTR(n.colnames, 2) = a.name 依賴 SYSCAT.INDEXES 的 COLNAMES 格式 (以 + 號分隔),若版本不同請驗證。