03/08: SQL > MS SQL 2000でデータベースの情報を取得する
Category: Z. etc
Posted by: hajime osako
SELECT DISTINCT C_TBL.TABLE_CATALOG, C_TBL.TABLE_SCHEMA, C_TBL.TABLE_NAME, C_TBL.COLUMN_NAME, C_TBL.IS_NULLABLE, C_TBL.DATA_TYPE, C_TBL.COLUMN_DEFAULT, C_TBL.CHARACTER_MAXIMUM_LENGTH, C_TBL.NUMERIC_PRECISION, C_TBL.NUMERIC_PRECISION_RADIX, C_TBL.NUMERIC_SCALE, PK_TBL.CONSTRAINT_TYPE, C_TBL.ORDINAL_POSITION FROM INFORMATION_SCHEMA.COLUMNS C_TBL LEFT OUTER JOIN (SELECT DISTINCT k.TABLE_CATALOG, k.TABLE_SCHEMA, k.TABLE_NAME, k.COLUMN_NAME, tc.constraint_type FROM information_schema.key_column_usage k INNER JOIN information_schema.table_constraints tc ON tc.constraint_name = k.constraint_name WHERE tc.constraint_type = 'PRIMARY KEY') PK_TBL ON C_TBL.TABLE_CATALOG = PK_TBL.TABLE_CATALOG AND C_TBL.TABLE_SCHEMA = PK_TBL.TABLE_SCHEMA AND C_TBL.TABLE_NAME = PK_TBL.TABLE_NAME AND C_TBL.COLUMN_NAME = PK_TBL.COLUMN_NAME ORDER BY C_TBL.TABLE_CATALOG, C_TBL.TABLE_SCHEMA, C_TBL.TABLE_NAME, C_TBL.ORDINAL_POSITION