开发者

Read Information_Schema of a SQL Table

开发者 https://www.devze.com 2022-12-20 07:06 出处:网络
I need to get s开发者_如何学JAVAchema details for the passed table name using information_schema.

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'
0

精彩评论

暂无评论...
验证码 换一张
取 消