读取SQL表的Information_Schema

时间:2010-02-11 00:30:21

标签: sql-server information-schema

我需要使用information_schema获取传递的表名的架构详细信息。 作为输出的一部分需要ColumnName,Constraint_Name,Constraint_Type。

如何做到这一点

由于

1 个答案:

答案 0 :(得分:0)

确定已解决此问题

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'