
SELECT

    t.TABLE_NAME AS [TableName],
    c.COLUMN_NAME AS [ColumnName],
    c.ORDINAL_POSITION AS [ColumnIndex],
    CAST(ISNULL(ep.[value], '') AS NVARCHAR(MAX)) AS [ColumnDescription],
    c.DATA_TYPE AS [ColumnType],
    CAST(CASE WHEN c.CHARACTER_MAXIMUM_LENGTH IS NULL THEN c.NUMERIC_PRECISION ELSE c.CHARACTER_MAXIMUM_LENGTH END AS NVARCHAR(10)) AS [ColumnSize],
    CAST(CASE WHEN EXISTS (
        SELECT 1 FROM HissAI_Res.INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
        INNER JOIN HissAI_Res.INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME AND tc.TABLE_SCHEMA = kcu.TABLE_SCHEMA AND tc.TABLE_NAME = kcu.TABLE_NAME
        WHERE tc.CONSTRAINT_TYPE = 'PRIMARY KEY'
        AND kcu.TABLE_SCHEMA = t.TABLE_SCHEMA
        AND kcu.TABLE_NAME = t.TABLE_NAME
        AND kcu.COLUMN_NAME = c.COLUMN_NAME
    ) THEN 1 ELSE 0 END AS BIT) AS [ColumnIsPK],
    CAST(CASE WHEN c.IS_NULLABLE = 'YES' THEN 1 ELSE 0 END AS BIT) AS [ColumnIsNull],
    CAST(COLUMN_DEFAULT AS NVARCHAR(MAX)) AS [ColumnDefault]
FROM
    HissAI_Res.INFORMATION_SCHEMA.TABLES t
INNER JOIN
    HissAI_Res.INFORMATION_SCHEMA.COLUMNS c ON t.TABLE_SCHEMA = c.TABLE_SCHEMA AND t.TABLE_NAME = c.TABLE_NAME
LEFT OUTER JOIN
    HissAI_Res.sys.columns sc ON t.TABLE_SCHEMA + '.' + t.TABLE_NAME = OBJECT_SCHEMA_NAME(sc.object_id) + '.' + OBJECT_NAME(sc.object_id) AND c.COLUMN_NAME = sc.name
LEFT OUTER JOIN
    HissAI_Res.sys.extended_properties ep ON t.TABLE_SCHEMA + '.' + t.TABLE_NAME = OBJECT_SCHEMA_NAME(ep.major_id) + '.' + OBJECT_NAME(ep.major_id) AND sc.column_id = ep.minor_id AND ep.name = 'MS_Description'
WHERE
    t.TABLE_NAME = 'T_App_Config'  and (ep.class=1 or    ep.class is null )
ORDER BY
    c.ORDINAL_POSITION ASC;

	