1.SQL Server 2000中查询表名 经常碰到一些忘记表名称的情况,此时只记得个大概,此时可通过查询系统表Sysobjects找到所要的表名,如要查找包含用户的表名,可通过以下SQL语句实现, Select * From sysobjects Where name like '%user%' 2.如果知道列名,想
1.SQL Server 2000中查询表名
经常碰到一些忘记表名称的情况,此时只记得个大概,此时可通过查询系统表Sysobjects找到所要的表名,如要查找包含用户的表名,可通过以下SQL语句实现,
Select *
From sysobjects
Where name like '%user%'
2.如果知道列名,想查找包含有该列的表名,可加上系统表syscolumns来实现,如想查找列名中包含有user的所有表名,可通过以下SQL语句来实现
Select *
From sysobjects s
Where Exists(
Select *
From syscolumns
Where ID = s.ID and name like '%user%'
)
3 .SQL SERVER
查看所有表名:
select name from sysobjects where type='U'
查询表的所有字段名:
Select name from syscolumns Where ID=OBJECT_ID('表名')
select * from information_schema.tables
select * from information_schema.views
select * from information_schema.columns
4.ACCESS
查看所有表名:
select name from MSysObjects where type=1 and flags=0
MSysObjects是系统对象,默认情况是隐藏的。通过工具、选项、视图、显示、系统对象可以使之显示出来。
1.获取表的基本字段属性
--获取SqlServer中表结构
SELECT syscolumns.name,systypes.name,syscolumns.isnullable,
syscolumns.length
FROM syscolumns, systypes
WHERE syscolumns.xusertype = systypes.xusertype
AND syscolumns.id = object_id('你的表名')
2.获取字段的描述信息
--获取SqlServer中表结构 主键,及描述
declare @table_name as varchar(max)
set @table_name = '你的表名'
select sys.columns.name, sys.types.name, sys.columns.max_length, sys.columns.is_nullable,
(select count(*) from sys.identity_columns where sys.identity_columns.object_id = sys.columns.object_id and sys.columns.column_id = sys.identity_columns.column_id) as is_identity ,
(select value from sys.extended_properties where sys.extended_properties.major_id = sys.columns.object_id and sys.extended_properties.minor_id = sys.columns.column_id) as description
from sys.columns, sys.tables, sys.types where sys.columns.object_id = sys.tables.object_id and sys.columns.system_type_id=sys.types.system_type_id and sys.tables.name=@table_name order by sys.columns.column_id
3.单独查询表的递增字段
--单独查询表递增字段
select [name] from syscolumns where
id=object_id(N'你的表名') and COLUMNPROPERTY(id,name,'IsIdentity')=1
4.获取表的主外键
--获取表主外键约束
exec sp_helpconstraint '你的表名' ;
5.相当完整的表结构查询
--很全面的表结构
exec sp_helpconstraint '你的表名' ;
SELECT 表名 = CASE a.colorder WHEN 1 THEN c.name ELSE '' END,
序 = a.colorder,
字段名= a.name,
标识 = CASE COLUMNPROPERTY(a.id,a.name,'IsIdentity') WHEN 1 THEN '√' ELSE '' END,
主键 = CASE
WHEN EXISTS ( SELECT * FROM sysobjects WHERE xtype='PK'
AND name IN (SELECT [name] FROM sysindexes WHERE id=a.id
AND indid IN (SELECT indid FROM sysindexkeys WHERE id=a.id
AND colid IN (SELECT colid FROM syscolumns WHERE id=a.id
AND name=a.name)))) THEN '√' ELSE '' END,
类型= b.name,
字节数= a.length,
长度 = COLUMNPROPERTY(a.id,a.name,'Precision'),
小数 = CASE ISNULL(COLUMNPROPERTY(a.id,a.name,'Scale'),0) WHEN 0 THEN '' ELSE CAST(COLUMNPROPERTY(a.id,a.name,'Scale') AS VARCHAR) END,
允许空= CASE a.isnullable WHEN 1 THEN '√' ELSE '' END,
默认值= ISNULL(d.[text],''),
说明 = ISNULL(e.[value],'')
FROM syscolumns a
LEFT JOIN systypes b ON a.xtype=b.xusertype
INNER JOIN sysobjects c ON a.id=c.id AND c.xtype='U' AND c.name'dtproperties'
LEFT JOIN syscomments d ON a.cdefault=d.id
LEFT JOIN sys.extended_properties e ON a.id=e.class AND a.colid=e.minor_id
ORDER BY c.name, a.colorder
6.获取所有的库名
--获取服务器中的所有库名
select * from mastersysdatabases
7.获取服务器上所有库的所有表
--获取服务器上所有库的所有表名
use master
declare @db_name varchar(100)
declare @sql varchar(200)
declare cur_tables cursor
for
select name from sysdatabases /*where name like 'by_%'*/
open cur_tables
fetch next from cur_tables into @db_name
while @@fetch_status = 0
begin
--set @db_name = @db_name + '.dbo.sysobjects'
print @db_name
set @sql = 'select * from ' + @db_name + '.dbo.sysobjects where xtype =''U'''
exec (@sql)
fetch next from cur_tables into @db_name
end
close cur_tables
deallocate cur_tables
go
1.按姓氏笔画排序:
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
2.分页SQL语句
select * from(select (row_number() OVER (ORDER BY tab.ID Desc)) as rownum,tab.* from 表名 As tab) As t where rownum between 起始位置 And 结束位置
3.获取当前数据库中的所有用户表
select * from sysobjects where xtype='U' and category=0
4.获取某一个表的所有字段
select name from syscolumns where id=object_id('表名')
5.查看与某一个表相关的视图、存储过程、函数
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'
6.查看当前数据库中所有存储过程
select name as 存储过程名称 from sysobjects where xtype='P'
7.查询用户创建的所有数据库
select * from mastersysdatabases D where sid not in(select sid from mastersyslogins where)
或者
select dbid, name AS DB_NAME from mastersysdatabases where sid 0x01
8.查询某一个表的字段和数据类型
select column_name,data_type from information_schema.columns
where table_name = '表名'
9.使用事务
在使用一些对数据库表的临时的SQL语句操作时,可以采用SQL SERVER事务处理,防止对数据操作后发现误操作问题
开始事务
Begin tran
Insert Into TableName Values(…)
SQL语句操作不正常,则回滚事务。
回滚事务
Rollback tran
SQL语句操作正常,则提交事务,数据提交至数据库。
提交事务
Commit tran
10. 按全文匹配方式查询
字段名 LIKE N'%[^a-zA-Z0-9]China[^a-zA-Z0-9]%'
OR 字段名 LIKE N'%[^a-zA-Z0-9]China'
OR 字段名 LIKE N'China[^a-zA-Z0-9]%'
OR 字段名 LIKE N'China
11.计算执行SQL语句查询时间
declare @d datetime
set @d=getdate()
select * from SYS_ColumnProperties select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())
12、说明:几个高级查询运算词
A: UNION 运算符