2013年5月19日 星期日

[MSSQL][SQL] Show Table MetaData


SELECT
  SCHEMA_NAME(T.schema_id) AS table_schema,
  T.name,
  KC.name AS constraint_name,
  COL_NAME(T.object_id, IC.column_id) AS column_name,
  IC.key_ordinal,
  IC.is_descending_key
FROM
  sys.tables AS T
  INNER JOIN
  sys.key_constraints AS KC
  ON KC.parent_object_id = T.object_id
  INNER JOIN
  sys.indexes AS I
  ON KC.unique_index_id = I.index_id
  AND KC.parent_object_id = I.object_id
  INNER JOIN
  sys.index_columns AS IC
  ON I.object_id = IC.object_id
  AND I.index_id = IC.index_id
WHERE
  T.type = 'U'
  AND IC.is_included_column = 0
  and T.[name] = @tablesname
ORDER BY
  T.object_id,
  IC.key_ordinal;

沒有留言: