Read Information_Schema of a SQL Table
I need to get s开发者_如何学JAVAchema details for the passed table name using information_schema. As part of output need ColumnName, Constraint_Name, Constraint_Type.
How can this be done
Thanks
OK SOLVED THIS AS
WITH CONSTRAINTLIST
AS
(
SELECT KCU.TABLE_NAME, KCU.COLUMN_NAME, TC.CONSTRAINT_TYPE
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU ON KCU.CONSTRAINT_SCHEMA = TC.CONSTRAINT_SCHEMA
AND KCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
AND KCU.TABLE_SCHEMA = TC.TABLE_SCHEMA
AND KCU.TABLE_NAME = TC.TABLE_NAME
WHERE TC.CONSTRAINT_TYPE IN ( 'PRIMARY KEY')
)
SELECT COL.COLUMN_NAME, CL.CONSTRAINT_TYPE FROM INFORMATION_SCHEMA.COLUMNS COL
LEFT JOIN CONSTRAINTLIST CL
ON COL.COLUMN_NAME = CL.COLUMN_NAME AND COL.TABLE_NAME = CL.TABLE_NAME
WHERE COL.TABLE_NAME = 'TABLE_NAME'
精彩评论