原文地址:http://blog.csdn.net/pukuimin1226/article/details/7687538
----查询数据库中用户创建的表
----jsj01 为数据库名
select name tablename from jsj01..sysobjects where type='U' and name not in ('dtproperties')
--查询表里的字段信息
exec sp_help 对象名
---docs为表名
select * from syscolumns where id = object_id('docs')
----查询数据库中所有类型
select name,xtype from systypes
----两表联查,显示表中所有字段和对应的数据类型
----syscolumns里字段‘xtype’ 对应 systypes里的 ‘xusertype’ ,systypes 里的‘name’字段就是字段的数据类型
----docs 为表名
select a.name as fieldname,b.name as type from syscolumns as a join systypes as b on a.xtype = b.xusertype where id=object_id('docs')
----docs为数据表名 : 查询表字段、类型、说明
select a.name fieldname,b.name type,c.value comment from syscolumns as a full join systypes as b on a.xtype = b.xusertype full join ::fn_listextendedproperty(NULL, 'user', 'dbo', 'table', 'docs', 'column', default) as c ----这是2000版本,2005把user改为schema on a.name=c.objname COLLATE Chinese_PRC_CI_AS -----排序规则(有时不加也可以,如果两表的排序规则不同,则会报错) --join sysproperties c --on a.id=c.major_id where id=object_id('docs')
----查询表里的主键,没有主键为空,如果是多个组合主键就有多个值 pk为主键 fk为外键
--- jsj01 为数据库名 docs为表名 fk表示外键
----sql2000
select column_name as primarykey,* from [jsj01].INFORMATION_SCHEMA.KEY_COLUMN_USAGE where Table_name='docs' and constraint_name like 'fk_%'
-----sql2005
SELECT @PrimaryKey = CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU ON TC.CONSTRAINT_NAME = CCU.CONSTRAINT_NAME WHERE TC.TABLE_NAME = 'docs' AND TC.CONSTRAINT_TYPE='PRIMARY KEY'
--select * from sysobjects WHERE OBJECT_NAME(sysobjects.parent_obj)='docs' --and xtype='pk' --select * from sysconstraints where id = object_id('docs') --select * from syscolumns where id = object_id('docs') --select * from sysindexes --select * from sysindexkeys
----查询表中自动增长的字段,没有为空,如果有就只有一个
----docs为表名 -- sql2000
SELECT a.name column_name,b.name data_type FROM syscolumns a,systypes b WHERE a.id=object_id('docs') and a.xtype = b.xusertype AND a.autoval is not null
-------- sql2005
select a.Name from syscolumns a left join sysobjects b on a.iD=b.parent_obj and b.xtype='PK' where a.ID=object_id('docs') and a.status=0x80
----For mssql2005
SELECT 表名=case when a.colorder=1 then d.name else '' end, 表说明=case when a.colorder=1 then isnull(f.value,'') else '' end, 字段序号=a.colorder, 字段名=a.name, 标识=case when COLUMNPROPERTY(a.id,a.name,'IsIdentity')=1 then '1' else '' end, 主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in( SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid ))) then '1' else '' end, 类型=b.name, 占用字节数=a.length, 长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'), 小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0), 允许空=case when a.isnullable=1 then '1' else '' end, 默认值=isnull(e.text,''), 字段说明=isnull(g.[value],'') FROM syscolumns a left join systypes b on a.xtype=b.xusertype inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' left join syscomments e on a.cdefault=e.id left join sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id left join sys.extended_properties f on d.id=f.major_id and f.minor_id =0 --where d.name='classinfo' --如果只查询指定表,加上此条件 order by a.id,a.colorder
----For mssql2000
SELECT 表名=d.name,--case when a.colorder=1 then d.name else '' end, 字段序号=a.colorder, 字段名=a.name, 标识=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end, 主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in ( SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid ))) then '√' else '' end, 类型=b.name, 占用字节数=a.length, 长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'), 小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0), 允许空=case when a.isnullable=1 then '√'else '' end, 默认值=isnull(e.text,''), 字段说明=isnull(g.[value],'') FROM syscolumns a left join systypes b on a.xtype=b.xusertype inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' left join syscomments e on a.cdefault=e.id left join sysproperties g on a.id=g.id and a.colid=g.smallid order by a.id,a.colorder