Skip to content

Instantly share code, notes, and snippets.

@hashmaparraylist
Created July 3, 2014 06:35
SQLServer 获取数据库表,字段的信息
SELECT
b.name as "Table",
a.name as "COLUMN",
c.name as "Datatype",
a.max_length as "Size",
a.precision,
a.scale ,
case when a.is_nullable =0 then 'NOT Null' else 'NUll' end as "NULL",
case when a.is_identity =0 then '×' else '○' end as "IDENTITY",
case when a.default_object_id = 0 then '×' else d.definition end as 'DEFAULT',
case when f.index_column_id is not null AND e.is_primary_key = 1 then 'PK' else '×' end as 'PK'
FROM
sys.columns as a
Left outer JOIN sys.objects as b ON a.object_id = b.object_id
Left outer JOIN sys.types as c ON a.system_type_id = c.system_type_id
Left outer JOIN sys.default_constraints as d ON a.default_object_id = d.object_id
Left outer JOIN sys.indexes as e ON a.object_id = e.object_id AND e.is_primary_key = 1
Left outer JOIN sys.index_columns as f ON a.object_id = f.object_id AND a.column_id = f.column_id
WHERE
b.type = 'U' AND
c.name != 'sysname' AND
c.name = 'varchar'
and (f.index_column_id is not null and e.is_primary_key = 1)
ORDER BY
b.name,
a.column_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment