取得sql sever 2005表的列信息(摘至LTP.Net)
SELECT C.column_id AS colorder, C.name AS ColumnName, T.name AS TypeName,
C.max_length AS Length, C.precision AS Preci, C.scale AS Scale,
CASE WHEN C.is_identity = 1 THEN N'√' ELSE N'' END AS IsIdentity,
ISNULL(IDX_1.PrimaryKey, N'') AS isPK,
CASE WHEN C.is_computed = 1 THEN N'√' ELSE N'' END AS Computed,
ISNULL(IDX_1.IndexName, N'') AS IndexName, ISNULL(IDX_1.Sort, N'') AS IndexSort,
O.create_date AS Create_Date, O.modify_date AS Modify_Date,
CASE WHEN C.is_nullable = 1 THEN N'√' ELSE N'' END AS cisNull,
ISNULL(D.definition, N'') AS defaultVal, ISNULL(PFD.value, N'') AS deText
FROM sys.columns AS C INNER JOIN
sys.objects AS O ON C.object_id = O.object_id AND O.type = 'U' AND
O.is_ms_shipped = 0 INNER JOIN
sys.types AS T ON C.user_type_id = T.user_type_id LEFT OUTER JOIN
sys.default_constraints AS D ON C.object_id = D.parent_object_id AND
C.column_id = D.parent_column_id AND
C.default_object_id = D.object_id LEFT OUTER JOIN
sys.extended_properties AS PFD ON PFD.class = 1 AND
C.object_id = PFD.major_id AND C.column_id = PFD.minor_id LEFT OUTER JOIN
sys.extended_properties AS PTB ON PTB.class = 1 AND PTB.minor_id = 0 AND
C.object_id = PTB.major_id LEFT OUTER JOIN
(SELECT IDXC.object_id, IDXC.column_id,
CASE INDEXKEY_PROPERTY(IDXC.[object_id], IDXC.index_id,
IDXC.index_column_id, 'IsDescending')
WHEN 1 THEN 'DESC' WHEN 0 THEN 'ASC' ELSE '' END AS Sort,
CASE WHEN IDX.is_primary_key = 1 THEN N'√' ELSE N'' END AS PrimaryKey,
IDX.name AS IndexName
FROM sys.indexes AS IDX INNER JOIN
sys.index_columns AS IDXC ON IDX.object_id = IDXC.object_id AND
IDX.index_id = IDXC.index_id LEFT OUTER JOIN
sys.key_constraints AS KC ON IDX.object_id = KC.parent_object_id AND
IDX.index_id = KC.unique_index_id INNER JOIN
(SELECT object_id, column_id, MIN(index_id) AS index_id
FROM sys.index_columns
GROUP BY object_id, column_id) AS IDXCUQ ON
IDXC.object_id = IDXCUQ.object_id AND
IDXC.column_id = IDXCUQ.column_id AND
IDXC.index_id = IDXCUQ.index_id) AS IDX_1 ON
C.object_id = IDX_1.object_id AND C.column_id = IDX_1.column_id
WHERE (O.name = N'users')
ORDER BY O.name, colorder
C.max_length AS Length, C.precision AS Preci, C.scale AS Scale,
CASE WHEN C.is_identity = 1 THEN N'√' ELSE N'' END AS IsIdentity,
ISNULL(IDX_1.PrimaryKey, N'') AS isPK,
CASE WHEN C.is_computed = 1 THEN N'√' ELSE N'' END AS Computed,
ISNULL(IDX_1.IndexName, N'') AS IndexName, ISNULL(IDX_1.Sort, N'') AS IndexSort,
O.create_date AS Create_Date, O.modify_date AS Modify_Date,
CASE WHEN C.is_nullable = 1 THEN N'√' ELSE N'' END AS cisNull,
ISNULL(D.definition, N'') AS defaultVal, ISNULL(PFD.value, N'') AS deText
FROM sys.columns AS C INNER JOIN
sys.objects AS O ON C.object_id = O.object_id AND O.type = 'U' AND
O.is_ms_shipped = 0 INNER JOIN
sys.types AS T ON C.user_type_id = T.user_type_id LEFT OUTER JOIN
sys.default_constraints AS D ON C.object_id = D.parent_object_id AND
C.column_id = D.parent_column_id AND
C.default_object_id = D.object_id LEFT OUTER JOIN
sys.extended_properties AS PFD ON PFD.class = 1 AND
C.object_id = PFD.major_id AND C.column_id = PFD.minor_id LEFT OUTER JOIN
sys.extended_properties AS PTB ON PTB.class = 1 AND PTB.minor_id = 0 AND
C.object_id = PTB.major_id LEFT OUTER JOIN
(SELECT IDXC.object_id, IDXC.column_id,
CASE INDEXKEY_PROPERTY(IDXC.[object_id], IDXC.index_id,
IDXC.index_column_id, 'IsDescending')
WHEN 1 THEN 'DESC' WHEN 0 THEN 'ASC' ELSE '' END AS Sort,
CASE WHEN IDX.is_primary_key = 1 THEN N'√' ELSE N'' END AS PrimaryKey,
IDX.name AS IndexName
FROM sys.indexes AS IDX INNER JOIN
sys.index_columns AS IDXC ON IDX.object_id = IDXC.object_id AND
IDX.index_id = IDXC.index_id LEFT OUTER JOIN
sys.key_constraints AS KC ON IDX.object_id = KC.parent_object_id AND
IDX.index_id = KC.unique_index_id INNER JOIN
(SELECT object_id, column_id, MIN(index_id) AS index_id
FROM sys.index_columns
GROUP BY object_id, column_id) AS IDXCUQ ON
IDXC.object_id = IDXCUQ.object_id AND
IDXC.column_id = IDXCUQ.column_id AND
IDXC.index_id = IDXCUQ.index_id) AS IDX_1 ON
C.object_id = IDX_1.object_id AND C.column_id = IDX_1.column_id
WHERE (O.name = N'users')
ORDER BY O.name, colorder