
1.库表列信息
--取所有库
SELECT Name FROM Master..SysDatabases ORDER BY Name --查询所有表
select name from 库名..sysobjects where xtype in( 'u','v') order by xtype,name --查询所有列
SELECT COLUMN_NAME FROM 库名.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_Name = '表名'
SELECT name FROM 库名..syscolumns WHERE id = Object_Id('表名')--这个不知为何有时不好用 可能.net中用的原因 查询还没出过错
2.查询表结构 无法跨库查询 使用 columnproperty() 函数
SELECT obj.name TblName,
col.colorder AS Code ,
col.name AS FldName ,
ISNULL(ep.[value], '') AS FldDescription ,
t.name AS DataType ,
col.length AS DataLenth ,
ISNULL(COLUMNPROPERTY(col.id, col.name, 'Scale'), 0) AS DecimalLenth ,
CASE WHEN COLUMNPROPERTY(col.id, col.name, 'IsIdentity') = 1 THEN ''
ELSE ''
END AS IsAutoIncrement ,
CASE WHEN EXISTS ( SELECT 1
FROM dbo.sysindexes si
INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id
AND si.indid = sik.indid
INNER JOIN dbo.syscolumns sc ON sc.id = sik.id
AND sc.colid = sik.colid
INNER JOIN dbo.sysobjects so ON so.name = si.name
AND so.xtype = 'PK'
WHERE sc.id = col.id
AND sc.colid = col.colid ) THEN ''
ELSE ''
END AS PrimaryKey ,
CASE WHEN col.isnullable = 1 THEN ''
ELSE ''
END AS AlowNull ,
ISNULL(comm.text, '') AS DefaultValue
FROM dbo.syscolumns col
LEFT JOIN dbo.systypes t ON col.xtype = t.xusertype
inner JOIN dbo.sysobjects obj ON col.id = obj.id
AND obj.xtype = 'U'
AND obj.status >= 0
LEFT JOIN dbo.syscomments comm ON col.cdefault = comm.id
LEFT JOIN sys.extended_properties ep ON col.id = ep.major_id
AND col.colid = ep.minor_id
AND ep.name = 'MS_Description'
LEFT JOIN sys.extended_properties epTwo ON obj.id = epTwo.major_id
AND epTwo.minor_id = 0
AND epTwo.name = 'MS_Description'
WHERE obj.name = 'tbl_Data_Schemes'--表名
ORDER BY col.colorder ;
3.填写列的备注信息
EXEC sp_addextendedproperty 'MS_Description', '说明性文字', 'user', dbo,
'table', 表名, 'column', 列名