代碼:
SELECT
col.ORDINAL_POSITION AS POSITION,
col.TABLE_NAME,
col.COLUMN_NAME,
col.DATA_TYPE,
col.CHARACTER_MAXIMUM_LENGTH AS MAX_LENGTH,
ISNULL(col.COLUMN_DEFAULT, '') AS COLUMN_DEFAULT,
col.IS_NULLABLE AS ALLOW_NULL,
ISNULL(des.DESCRIPTION, '') AS DESCRIPTION,
ISNULL(pk.CONSTRAINT_TYPE, '') AS PRIMARY_KEY,
ISNULL(fk.CONSTRAINT_TYPE, '') AS FOREIGN_KEY
FROM
INFORMATION_SCHEMA.COLUMNS col
LEFT OUTER JOIN
(
SELECT
OBJECT_NAME(c.object_id) as TABLE_NAME,
c.NAME as COLUMN_NAME,
ex.VALUE as DESCRIPTION
FROM
sys.columns c
LEFT OUTER JOIN
sys.extended_properties ex
ON
ex.major_id = c.object_id
AND ex.minor_id = c.column_id
AND ex.name = 'MS_Description'
WHERE
OBJECTPROPERTY(c.object_id, 'IsMsShipped') = 0
) as des
ON
col.TABLE_NAME = des.TABLE_NAME and
col.COLUMN_NAME = des.COLUMN_NAME
LEFT OUTER JOIN
(
select
pk.TABLE_NAME,
c.COLUMN_NAME,
pk.CONSTRAINT_TYPE
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk
inner join INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
on
c.TABLE_NAME = pk.TABLE_NAME and
c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME
) as pk
ON
col.TABLE_NAME = pk.TABLE_NAME and
col.COLUMN_NAME = pk.COLUMN_NAME and
pk.CONSTRAINT_TYPE = 'PRIMARY KEY'
LEFT OUTER JOIN
(
select
pk.TABLE_NAME,
c.COLUMN_NAME,
pk.CONSTRAINT_TYPE
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk
inner join INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
on
c.TABLE_NAME = pk.TABLE_NAME and
c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME
) as fk
ON
col.TABLE_NAME = fk.TABLE_NAME and
col.COLUMN_NAME = fk.COLUMN_NAME and
fk.CONSTRAINT_TYPE = 'FOREIGN KEY'
WHERE
col.TABLE_NAME LIKE '%'
ORDER BY
col.TABLE_NAME,
POSITION
col.ORDINAL_POSITION AS POSITION,
col.TABLE_NAME,
col.COLUMN_NAME,
col.DATA_TYPE,
col.CHARACTER_MAXIMUM_LENGTH AS MAX_LENGTH,
ISNULL(col.COLUMN_DEFAULT, '') AS COLUMN_DEFAULT,
col.IS_NULLABLE AS ALLOW_NULL,
ISNULL(des.DESCRIPTION, '') AS DESCRIPTION,
ISNULL(pk.CONSTRAINT_TYPE, '') AS PRIMARY_KEY,
ISNULL(fk.CONSTRAINT_TYPE, '') AS FOREIGN_KEY
FROM
INFORMATION_SCHEMA.COLUMNS col
LEFT OUTER JOIN
(
SELECT
OBJECT_NAME(c.object_id) as TABLE_NAME,
c.NAME as COLUMN_NAME,
ex.VALUE as DESCRIPTION
FROM
sys.columns c
LEFT OUTER JOIN
sys.extended_properties ex
ON
ex.major_id = c.object_id
AND ex.minor_id = c.column_id
AND ex.name = 'MS_Description'
WHERE
OBJECTPROPERTY(c.object_id, 'IsMsShipped') = 0
) as des
ON
col.TABLE_NAME = des.TABLE_NAME and
col.COLUMN_NAME = des.COLUMN_NAME
LEFT OUTER JOIN
(
select
pk.TABLE_NAME,
c.COLUMN_NAME,
pk.CONSTRAINT_TYPE
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk
inner join INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
on
c.TABLE_NAME = pk.TABLE_NAME and
c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME
) as pk
ON
col.TABLE_NAME = pk.TABLE_NAME and
col.COLUMN_NAME = pk.COLUMN_NAME and
pk.CONSTRAINT_TYPE = 'PRIMARY KEY'
LEFT OUTER JOIN
(
select
pk.TABLE_NAME,
c.COLUMN_NAME,
pk.CONSTRAINT_TYPE
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk
inner join INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
on
c.TABLE_NAME = pk.TABLE_NAME and
c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME
) as fk
ON
col.TABLE_NAME = fk.TABLE_NAME and
col.COLUMN_NAME = fk.COLUMN_NAME and
fk.CONSTRAINT_TYPE = 'FOREIGN KEY'
WHERE
col.TABLE_NAME LIKE '%'
ORDER BY
col.TABLE_NAME,
POSITION