特定のデータベースの列に関する特定の重要な情報を提供するクエリを作成しようとしていますが、これまでのところ、クエリが奇妙な結果を返すようです! 列のサイズ、null 可能性、一意性などについて知る必要があります。
では、なぜ列ごとに複数の結果が得られるのでしょうか?
SELECT
C.COLUMN_NAME AS COLUMN_NAME,
C.TABLE_NAME AS TABLE_NAME,
C.CHARACTER_MAXIMUM_LENGTH AS CHARACTER_MAXIMUM_LENGTH,
C.COLUMN_DEFAULT AS COLUMN_DEFAULT,
C.DATA_TYPE AS DATA_TYPE,
C.IS_NULLABLE AS IS_NULLABLE,
CASE WHEN EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
AND CONSTRAINT_NAME = CC.CONSTRAINT_NAME) THEN 1 ELSE 0 END AS IS_PRIMARY_KEY,
CASE WHEN EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'UNIQUE'
AND CONSTRAINT_NAME = CC.CONSTRAINT_NAME) THEN 1 ELSE 0 END AS IS_UNIQUE,
C.NUMERIC_PRECISION AS NUMERIC_PRECISION,
C.NUMERIC_SCALE AS NUMERIC_SCALE,
FK.TABLE_NAME AS FOREIGN_KEY_TABLE_NAME,
FK.COLUMN_NAME AS FOREIGN_KEY_COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS C
LEFT OUTER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CC
ON C.COLUMN_NAME = CC.COLUMN_NAME
AND C.TABLE_NAME = CC.TABLE_NAME
LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
ON CC.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
LEFT OUTER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
ON TC.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
LEFT OUTER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE FC
ON RC.UNIQUE_CONSTRAINT_NAME = FC.CONSTRAINT_NAME
LEFT OUTER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE FK
ON FC.COLUMN_NAME = FK.COLUMN_NAME
AND FC.TABLE_NAME = FK.TABLE_NAME
WHERE COLUMNPROPERTY(OBJECT_ID(C.TABLE_SCHEMA + '.' + C.TABLE_NAME), C.COLUMN_NAME, 'IsComputed') = 0
AND TC.CONSTRAINT_TYPE = 'FOREIGN KEY'