MS SQL Server 查询元数据


use test
-- 查询数据库中所有的表和架构名称
select SCHEMA_NAME(schema_id) as table_schema_name, name as table_name from sys.tables;

select table_schema,table_name from INFORMATION_SCHEMA.TABLES where TABLE_TYPE=N'base table'

-- 查看表列信息
select name as column_name,
TYPE_NAME(system_type_id)as data_type,
max_length,
collation_name,is_nullable from sys.all_columns where object_id = object_id(N'UserInfo')

select column_name,data_type,character_maximum_length,collation_name,is_nullable from INFORMATION_SCHEMA.COLUMNS

-- 系统存储过程和函数

-- 通过存储过程返回一个当前数据库的对象列表
exec sys.sp_tables

-- 返回对象常用信息的多个结果集以及列、索引、约束等
exec sys.sp_help @objname='userInfo'

-- 查询表中列的信息
exec sys.sp_columns @table_name=N'userInfo',@table_owner=N'dbo'

-- 返回对象的约束信息
exec sys.sp_helpconstraint @objname=N'dbo.userInfo'

-- 返回本实例的版本信息
select SERVERPROPERTY('productlevel')

-- 返回数据库的排序规则
select DATABASEPROPERTYEX('Test','collation')

-- 返回指定对象所查询的属性 例如:查询userInfo表是否有主键

select OBJECTPROPERTY(OBJECT_ID('userInfo'),'TableHasPrimarykey')

-- 返回指定列所查询的信息 例如查询UserName是否可以为空
select COLUMNPROPERTY(OBJECT_ID('UserInfo'),'UserName','allowsnull')

posted @ 2016-06-28 22:37  Wǒ々啊申々  阅读(337)  评论(0编辑  收藏  举报