CREATE PROC Platform_GetTableSchema
@tablename varchar(100)
AS
BEGIN
SELECT column_name INTO #pkTable
FROM (SELECT constraint_name,column_name FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
WHERE table_name=@tablename) t1
JOIN (SELECT id FROM sysobjects
WHERE xtype=N'PK') t2
ON OBJECT_ID(constraint_name)=id
SELECT
d.name AS 表名,
a.colorder AS 列索引,
a.name AS 列名,
b.name AS 类型,
COLUMNPROPERTY(a.id,a.name,'PRECISION') AS 长度,
ISNULL(COLUMNPROPERTY(a.id,a.name,'Scale'),0) AS 小数位数,
(COLUMNPROPERTY(a.id,a.name,'IsIdentity')) 标识,
(CASE WHEN p.column_name IS NOT NULL THEN 1 ELSE 0 END) 主键,
(COLUMNPROPERTY(a.id,a.name,'IsComputed')) 计算列,
k.definition 公式,
ISNULL(a.isnullable,'') 允许空,
ISNULL(e.text,'') 默认值,
ISNULL(g.[value],'') AS 字段说明
--a代表列集合表:为每个表和视图中的每列返回一行,并为数据库中的存储过程的每个参数返回一行
--b代表列类型表:为数据库中定义的每种系统提供的数据类型和每种用户定义的数据类型返回一行。
FROM syscolumns a
LEFT JOIN systypes b ON a.xtype=b.xusertype --d代表对象表:在数据库中创建的每个对象(例如约束、默认值、日志、规则以及存储过程)都对应一行
INNER JOIN sysobjects d ON a.id=d.id AND d.xtype='U' AND d.name<>'dtproperties' --e代表e.text是默认值:包含数据库中每个视图、规则、默认值、触发器、CHECK 约束、DEFAULT 约束和存储过程的项
LEFT JOIN syscomments e ON a.cdefault=e.id --g代表g.[value]是字段说明:针对当前数据库中的每个扩展属性返回一行。
LEFT JOIN sys.extended_properties g ON a.id=g.major_id AND a.colid = g.minor_id
LEFT JOIN #pkTable p ON a.name = p.column_name
LEFT JOIN (SELECT name,definition FROM sys.computed_columns WHERE object_id=object_id(@tablename)) k ON a.name=k.name
WHERE d.name = @tablename
ORDER BY a.id,a.colorder
END