
SELECT
    col.object_id ,
    obj.name AS table_name,
    col.name AS column_name,
    ISNULL(ep.value,'') AS column_description,
	inf.DATA_TYPE as column_type,
	ISNULL(inf.CHARACTER_MAXIMUM_LENGTH ,0) as column_length,
	indx.name
FROM
    sys.objects AS obj
INNER JOIN
    sys.columns AS col
ON
    col.object_id = obj.object_id
LEFT JOIN
    sys.extended_properties AS ep--注释
ON
    ep.major_id = obj.object_id
    AND ep.minor_id = col.column_id
    AND ep.class = 1
left join 
INFORMATION_SCHEMA.COLUMNS inf
on 
inf.TABLE_NAME=obj.name and 
inf.COLUMN_NAME=col.name
left join  sys.indexes indx
on 
 indx.object_id = obj.object_id
WHERE
    obj.type = 'U' and obj.name='T_App_Config'
ORDER BY
    table_name ASC, col.column_id ASC;


SELECT 
    T.TABLE_NAME AS 表名称,
	c.ORDINAL_POSITION as 字段顺序,
    C.COLUMN_NAME AS 字段名称,
    CASE 
        WHEN PT.VALUE IS NULL THEN ''
        ELSE PT.VALUE 
    END AS 字段注释,
    C.DATA_TYPE AS 字段类型,
    C.CHARACTER_MAXIMUM_LENGTH AS 字段大小,
    CASE 
        WHEN KCU.COLUMN_NAME IS NULL THEN 0
        ELSE 1 
    END AS 是否主键
FROM INFORMATION_SCHEMA.TABLES T
INNER JOIN INFORMATION_SCHEMA.COLUMNS C ON T.TABLE_NAME = C.TABLE_NAME
LEFT JOIN sys.extended_properties PT ON PT.major_id = OBJECT_ID(T.TABLE_SCHEMA + '.' + T.TABLE_NAME) AND PT.minor_id = C.ORDINAL_POSITION AND PT.NAME ='MS_Description'
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON KCU.TABLE_NAME = T.TABLE_NAME AND KCU.COLUMN_NAME = C.COLUMN_NAME AND EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC WHERE TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME)
WHERE T.TABLE_TYPE = 'BASE TABLE' AND T.TABLE_NAME ='T_App_Config'  
ORDER BY T.TABLE_NAME, C.ORDINAL_POSITION;



SELECT
    t.TABLE_NAME AS [表名],
    c.COLUMN_NAME AS [字段名],
    c.ORDINAL_POSITION AS [字段顺序],
    CAST(ISNULL(ep.[value], '') AS NVARCHAR(MAX)) AS [字段注释],
    c.DATA_TYPE AS [字段类型],
    CAST(CASE WHEN c.CHARACTER_MAXIMUM_LENGTH IS NULL THEN c.NUMERIC_PRECISION ELSE c.CHARACTER_MAXIMUM_LENGTH END AS NVARCHAR(10)) AS [字段大小],
    CAST(CASE WHEN EXISTS (
        SELECT 1 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
        INNER JOIN 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 [是否主键],
    CAST(CASE WHEN c.IS_NULLABLE = 'YES' THEN 1 ELSE 0 END AS BIT) AS [是否可空],
    CAST(COLUMN_DEFAULT AS NVARCHAR(MAX)) AS [默认值]
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'
ORDER BY
    c.ORDINAL_POSITION ASC;


	SELECT name FROM sys.databases where　[name] like '%HissAI%' ;
 

 SELECT
    obj.name AS table_name,
    ISNULL(ep.value,'') AS table_description
FROM
    HissAI_UserData.sys.objects AS obj
LEFT JOIN
    HissAI_UserData.sys.extended_properties AS ep
ON
    ep.major_id = obj.object_id
    AND ep.minor_id = 0
    AND ep.class = 1
WHERE
    obj.type = 'U'
ORDER BY
    table_name ASC;

		SELECT
			obj.name AS TableName,
		ISNULL(ep.value,'') AS TableDescription
	FROM
		HissAI_UserData.sys.objects AS obj
	LEFT JOIN
		HissAI_UserData.sys.extended_properties AS ep
	ON
		ep.major_id = obj.object_id
		AND ep.minor_id = 0
		AND ep.class = 1
	WHERE
		obj.type = 'U'
	ORDER BY
		TableName ASC;