综合网络资料整理]
1.获取所有数据库名:(1)、SelectName FROM Master..SysDatabases order by Name
2.获取所有表名:(1)、SelectName FROM SysObjects Where XType='U' orDER BY Name
XType='U':表示所有用户表;
XType='S':表示所有系统表;
(2)、SELECTname FROM sysobjects WHERE type = 'U' AND sysstat = '83'
注意:一般情况只需要type = 'U',但有时候会有系统表混在其中(不知道什么原因),加上后面一句后就能删除这些系统表了
3.获取所有字段名:(1)、SelectName FROM SysColumns Where id=Object_Id('TableName')
(2)、SELECTsyscolumns.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、得到表中主键所包含的列名:
SELECTsyscolumns.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代码
selectsyscolumns.name, systypes.name, syscolumns.length from syscolumns
left join systypes on syscolumns. xusertype=systypes. xusertype
where id=(select id from sysobjects where name='订货主档');
go;
或者用这样的写法,执行结果一样:
selectsyscolumns.name,systypes.name,syscolumns.length from syscolumns,systypes
where(syscolumns.id=object_id('订货主档')and syscolumns.xusertype=systypes.xusertype)
orderby syscolumns.colorder;
go
执行结果:(字段只出现一次,正常)
订单号码int4
客户编号nvarchar10
员工编号int4
订单日期datetime8
要货日期datetime8
送货日期datetime8
送货方式int4
运费money8
收货人nvarchar80
送货地址nvarchar120
送货城市nvarchar30
送货行政区nvarchar30
送货邮政编码nvarchar20
送货国家地区nvarchar30
selectsyscolumns.name, systypes.name, syscolumns.length from syscolumns
left join systypes on syscolumns.xtype=systypes.xtype
where id=(select id from sysobjects where name='订货主档');
go;
执行结果:(部分字段出现两次,数据类型不同)
订单号码int4
客户编号nvarchar10
客户编号sysname10
员工编号int4
订单日期datetime8
订单日期出生日期类型8
要货日期datetime8
要货日期出生日期类型8
送货日期datetime8
送货日期出生日期类型8
送货方式int4
运费money8
运费薪水类型8
收货人nvarchar80
收货人sysname80
送货地址nvarchar120
送货地址sysname120
送货城市nvarchar30
送货城市sysname30
送货行政区nvarchar30
送货行政区sysname30
送货邮政编码nvarchar20
送货邮政编码sysname20
送货国家地区nvarchar30
送货国家地区sysname30
查询存储过程DepartmentSalaryInfo所有的信息,信息包含在系统视图syscolumns,systypes中
selectsyscolumns.*, systypes.* from syscolumns
left join systypes on syscolumns.xusertype=systypes.xusertype
where id=(select id from sysobjects where name='DepartmentSalaryInfo');
go
execsp_procedure_params_rowset@procedure_name='DepartmentSalaryInfo';
go
执行结果:
北风贸易dboDepartmentSalaryInfo;1@RETURN_VALUE040NULL03NULLNULL10NULLNULLintint
北风贸易dboDepartmentSalaryInfo;1@department110NULL11291010NULLNULLNULLvarcharvarchar
北风贸易dboDepartmentSalaryInfo;1@average220NULL16NULLNULL19NULLNULLmoneymoney
北风贸易dboDepartmentSalaryInfo;1@maximum320NULL16NULLNULL19NULLNULLmoneymoney
北风贸易dboDepartmentSalaryInfo;1@minimum420NULL16NULLNULL19NULLNULLmoneymoney
--存储过程中的参数名,参数类型,参数长度
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';
查询数据库存储量大小 (Master)
DECLARE @tablespaceinfo TABLE (
nameinfo varchar(50),
rowsinfo int,
reserved varchar(20),
datainfo varchar(20),
index_size varchar(20),
unused varchar(20)
)
DECLARE @tablename varchar(255);
DECLARE Info_cursor CURSOR FOR
SELECT [name] FROM sys.tables WHERE type='U';
OPEN Info_cursor
FETCH NEXT FROM Info_cursor INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
insert into @tablespaceinfo exec sp_spaceused @tablename
FETCH NEXT FROM Info_cursor
INTO @tablename
END
CLOSE Info_cursor
DEALLOCATE Info_cursor
SELECT * FROM @tablespaceinfo
ORDER BY Cast(Replace(reserved,'KB','') as INT) DESC