分类: 数据库开发技术
2011-09-13 15:26:29
SELECT sysobjects.name AS tableName, syscolumns.name AS field, properties.[value] AS fieldRemark, systypes.name AS type,
syscolumns.length, ISNULL(COLUMNPROPERTY(syscolumns.id, syscolumns.name, 'Scale'), 0) AS [decimal], syscolumns.isnullable AS isnulls,
CASE WHEN syscomments.text IS NULL THEN '' ELSE syscomments.text END AS [Default],
CASE WHEN COLUMNPROPERTY(syscolumns.id, syscolumns.name, 'IsIdentity') = 1 THEN 'Y' ELSE 'N' END AS [id], 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 = syscolumns.id AND colid = syscolumns.colid)))
THEN 'Y' ELSE 'N' END AS PK FROM syscolumns INNER JOIN sysobjects ON sysobjects.id = syscolumns.id INNER JOIN
systypes ON syscolumns.xtype = systypes.xtype LEFT OUTER JOIN sysproperties properties ON syscolumns.id = properties.id AND
syscolumns.colid = properties.smallid LEFT OUTER JOIN sysproperties ON sysobjects.id = sysproperties.id AND
sysproperties.smallid = 0 LEFT OUTER JOIN syscomments ON syscolumns.cdefault = syscomments.id
WHERE (sysobjects.xtype = 'U') and systypes.name<>'sysname' order by sysobjects.name
使用:把这段代码拷贝到查询分析器内选中相关查询的数据库名即可。
//符合我自己使用的查询数据库字典方法:SQL server 200
SELECT sysobjects.name AS 表名,--获取数据库表名
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 = syscolumns.id AND colid = syscolumns.colid)))
THEN 'K' ELSE '' END AS PK,--是否是主键
syscolumns.name AS 字段名,
properties.[value] AS 字段描述,
systypes.name AS 数据类型,
syscolumns.length AS 长度,
CASE syscolumns.isnullable WHEN '1' THEN '是' ELSE '否'END AS 允许为空,
CASE WHEN syscomments.text IS NULL THEN '' ELSE syscomments.text END AS 默认值
FROM syscolumns INNER JOIN
sysobjects ON sysobjects.id = syscolumns.id INNER JOIN
systypes ON syscolumns.xtype = systypes.xtype LEFT OUTER JOIN
sysproperties properties ON syscolumns.id = properties.id AND
syscolumns.colid = properties.smallid LEFT OUTER JOIN
sysproperties ON sysobjects.id = sysproperties.id AND sysproperties.smallid = 0 LEFT OUTER JOIN
syscomments ON syscolumns.cdefault = syscomments.id
WHERE (sysobjects.xtype = 'U') and systypes.name<>'sysname'
order by sysobjects.name desc
上边是查询2000 的,2005提示有错误 “对象名 dbo.sysproperties 无效” 使用下边的没有问题。
SELECT TOP 100 PERCENT --a.id, CASE WHEN a.colorder = 1 THEN d.name ELSE '' END AS 表名, CASE WHEN a.colorder = 1 THEN isnull(f.value, '') ELSE '' END AS 表说明, a.colorder AS 字段序号, a.name AS 字段名, CASE WHEN COLUMNPROPERTY(a.id, a.name, 'IsIdentity') = 1 THEN '√' ELSE '' END AS 标识, 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 = so.name AND so.xtype = 'PK' WHERE sc.id = a.id AND sc.colid = a.colid) THEN '√' ELSE '' END AS 主键, b.name AS 类型, a.length AS 长度, COLUMNPROPERTY(a.id, a.name, 'PRECISION') AS 精度, ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) AS 小数位数, CASE WHEN a.isnullable = 1 THEN '√' ELSE '' END AS 允许空, ISNULL(e.text, '') AS 默认值, ISNULL(g.[value], '') AS 字段说明, d.crdate AS 创建时间, CASE WHEN a.colorder = 1 THEN d.refdate ELSE NULL END AS 更改时间 FROM dbo.syscolumns a LEFT OUTER JOIN dbo.systypes b ON a.xtype = b.xusertype INNER JOIN dbo.sysobjects d ON a.id = d.id AND d.xtype = 'U' AND d.status >= 0 LEFT OUTER JOIN dbo.syscomments e ON a.cdefault = e.id LEFT OUTER JOIN sys.extended_properties g ON a.id = g.major_id AND a.colid = g.minor_id LEFT OUTER JOIN sys.extended_properties f ON d.id = f.major_id AND f.minor_id = 0 ORDER BY d.name, a.colorder
1.获取所有数据库名:
(1)、Select Name FROM Master..SysDatabases order by Name
2.获取所有表名:
(1)、Select Name FROM SysObjects Where XType='U' orDER BY Name
XType='U':表示所有用户表;
XType='S':表示所有系统表;
(2)、SELECT name FROM sysobjects WHERE type = 'U' AND sysstat = '83'
注意:一般情况只需要type = 'U',但有时候会有系统表混在其中(不知道什么原因),加上后面一句后就能删除这些系统表了
3.获取所有字段名:
(1)、Select Name FROM SysColumns Where id=Object_Id('TableName')
(2)、SELECT syscolumns.name,systypes.name,syscolumns.isnullable,syscolumns.length FROM syscolumns, systypes WHERE syscolumns.xusertype = systypes.xusertype AND "syscolumns.id = object_id('tableName')
注意点:
(a)这里为了重点突出某些重要内容,选取了其中几项信息输出。
(b)syscolumns表中只含有数据类型编号,要获取完整的名字需要从systypes表中找,一般用户使用的数据类型用xusertype对应比较好,不会出现一对多的情况。
(c)syscolumns.length得到的是物理内存的长度,所以nvarchar和varchar等类型在数据库中的显示是这个的一半。
4、得到表中主键所包含的列名:
SELECT syscolumns.name FROM syscolumns,sysobjects,sysindexes,sysindexkeys WHERE syscolumns.id = object_id('tablename') AND sysobjects.xtype = 'PK' AND sysobjects.parent_obj = syscolumns.id AND sysindexes.id = syscolumns.id AND sysobjects.name = sysindexes.name AND sysindexkeys.id = syscolumns.id AND sysindexkeys.indid = sysindexes.indid AND syscolumns.colid = sysindexkeys.colid
注意:这是在4张系统表中寻找的,关系比较复杂,大致可以表示为:
syscolumns中存有表中的列信息和表id,sysobjects表中存有主键名字(即PK_Table类似)和表id,sysindexes中存 有主键名字和表id和index编号,sysindexkeys中存有表id和index编号和列编号,一项一项对应起来后就能找到列名了。
另外的SQL代码
select syscolumns.name, systypes.name, syscolumns.length from syscolumns
left join systypes on syscolumns. xusertype =systypes. xusertype
where id=(select id from sysobjects where name='订货主档');
go;
或者用这样的写法,执行结果一样:
select syscolumns.name, systypes.name, syscolumns.length from syscolumns,systypes
where (syscolumns.id=object_id('订货主档') and syscolumns.xusertype=systypes.xusertype)
order by syscolumns.colorder;
go
执行结果:(字段只出现一次,正常)
订单号码 int 4
客户编号 nvarchar 10
员工编号 int 4
订单日期 datetime 8
要货日期 datetime 8
送货日期 datetime 8
送货方式 int 4
运费 money 8
收货人 nvarchar 80
送货地址 nvarchar 120
送货城市 nvarchar 30
送货行政区 nvarchar 30
送货邮政编码 nvarchar 20
送货国家地区 nvarchar 30
select syscolumns.name, systypes.name, syscolumns.length from syscolumns
left join systypes on syscolumns.xtype=systypes.xtype
where id=(select id from sysobjects where name='订货主档');
go;
执行结果:(部分字段出现两次,数据类型不同)
订单号码 int 4
客户编号 nvarchar 10
客户编号 sysname 10
员工编号 int 4
订单日期 datetime 8
订单日期 出生日期类型 8
要货日期 datetime 8
要货日期 出生日期类型 8
送货日期 datetime 8
送货日期 出生日期类型 8
送货方式 int 4
运费 money 8
运费 薪水类型 8
收货人 nvarchar 80
收货人 sysname 80
送货地址 nvarchar 120
送货地址 sysname 120
送货城市 nvarchar 30
送货城市 sysname 30
送货行政区 nvarchar 30
送货行政区 sysname 30
送货邮政编码 nvarchar 20
送货邮政编码 sysname 20
送货国家地区 nvarchar 30
送货国家地区 sysname 30
查询存储过程DepartmentSalaryInfo所有的信息,信息包含在系统视图syscolumns,systypes中
select syscolumns.*, systypes.* from syscolumns
left join systypes on syscolumns.xusertype=systypes.xusertype
where id=(select id from sysobjects where name='DepartmentSalaryInfo');
go
exec sp_procedure_params_rowset @procedure_name = 'DepartmentSalaryInfo';
go
执行结果:
北风贸易 dbo DepartmentSalaryInfo;1 @RETURN_VALUE 0 4 0 NULL 0 3 NULL NULL 10 NULL NULL int int
北风贸易 dbo DepartmentSalaryInfo;1 @department 1 1 0 NULL 1 129 10 10 NULL NULL NULL varchar varchar
北风贸易 dbo DepartmentSalaryInfo;1 @average 2 2 0 NULL 1 6 NULL NULL 19 NULL NULL money money
北风贸易 dbo DepartmentSalaryInfo;1 @maximum 3 2 0 NULL 1 6 NULL NULL 19 NULL NULL money money
北风贸易 dbo DepartmentSalaryInfo;1 @minimum 4 2 0 NULL 1 6 NULL NULL 19 NULL NULL money money
--存储过程中的参数名,参数类型,参数长度
select syscolumns.name, systypes.name, syscolumns.length from syscolumns
left join systypes on syscolumns.xusertype=systypes.xusertype
where id=(select id from sysobjects where name='DepartmentSalaryInfo');
1:获取当前数据库中的所有用户表
select Name from sysobjects where xtype='u' and status>=0 2:获取某一个表的所有字段 select name from syscolumns where id=object_id('表名') 3:查询用户创建的所有数据库 select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name='sa') 或者 select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01 4:查询某一个表的字段和数据类型 select column_name,data_type from information_schema.columns where table_name = '表名' [n].[标题]: Select * From TableName Order By CustomerName [n].[标题]: 8.如何修改数据库的名称: sp_renamedb 'old_name', 'new_name' 9.只复制一个表结构,不复制数据 select top 0 * into [t1] from [t2] 10.连接远程数据库 select * from OPENDATASOURCE('SQLOLEDB','Data Source=远程ip;User ID=sa;Password=密码').库名.dbo.表名 11.获取当前oracle数据库中的所有表
select table_name from user_tables 12 .获取当前oracle表中所有字段的类型
SELECT
COLUMN_NAME,DATA_TYPE,DATA_LENGTH,NULLABLE FROM USER_TAB_COLS where TABLE_NAME='teacher'; |