您当前的位置: AG亚游集团 > 数据库教程 > MSSQL教程 > SqlServer常用对象查询

SqlServer常用对象查询

作者:xiaoxiao 来源:未知 发布时间: 2014-07-30 11:46 点击: 次
-- 查询用户表对象信息 select Tab . Name as [ 表名 ] , Tab . create_date as [ 创建时间 ] , Tab . modify_date as [ 最后修改时间 ] , Col . Name as [ 列名 ] , Type . name as [ 数据类型 ] , Col . max_length as [ 字段长度 ] , CASE WHEN pk . is_primary_key

AG亚游集团,  还穿着跟前女友去的情侣衣舍不得扔,钱包里放着跟前女友看的票,这些都说明他还忘不了前女友。

作为这份报告的发起方,金立手机扎根行业十四年,从注重产品科技研发,到以用户需求为中心,金立不仅关注高新科技发展,更注重人文精神关怀,"越亲密越快乐、越有爱越快乐、越关心越快乐",这是金立"科技悦生活"主张的含义,更是金立向其高端政商用户所传递的美好愿景。 品牌设计公司排行榜  今日上午,马云发出微博《致两会代表委员们:像治理酒驾那样治理假货》。马云称,最近关于打假的讨论越来越热烈,包括一些人大代表的建议议案,这样的讨论很。就像五年前,如果没有一场关于酒驾的大讨论,没有经争论形成全社会的共识,就不会有后来的司法成果和社会进步。

SqlServer常用对象查询

  --查询用户表对象信息

  select Tab.Name as [表名],Tab.create_date as [创建时间],Tab.modify_date as [最后修改时间],

  Col.Name as [列名] ,Type.name as [数据类型],Col.max_length as [字段长度],

  CASE WHEN pk.is_primary_key= 1 THEN 'Y' ELSE 'N' end as [是否主键],

  CASE WHEN Col.is_identity = 1 THEN 'Y' else 'N'end as [是否自增] ,

  identity_columns.seed_value as [自增种子],identity_columns.increment_value as [自增步长],

  case when Col.is_nullable = 1 then 'Y' else 'N' END AS [是否允许为NULL],

  Def.text as [默认值],case when Col.is_computed = 1 then 'Y' else 'N' END as [是否计算列] ,

  computed_columns.definition as [计算公式],Col_Desc.Value as [列备注]

  from sys.objects Tab inner join sys.columns Col on Tab.object_id =Col.object_id

  inner join sys.types Type on Col.system_type_id = Type.system_type_id

  left join sys.identity_columns identity_columns on Tab.object_id = identity_columns.object_id and Col.column_id = identity_columns.column_id

  left join syscomments Def on Col.default_object_id = Def.ID

  left join(

  select index_columns.object_id,index_columns.column_id,indexes.is_primary_key

  from sys.indexes indexes inner join sys.index_columns index_columns

  on indexes.object_id = index_columns.object_id and indexes.index_id = index_columns.index_id

  where indexes.is_primary_key = 1/*主键*/

  ) PK on Tab.object_id = PK.object_id AND Col.column_id = PK.column_id

  left join sys.computed_columns computed_columns on Tab.object_id =computed_columns.object_id and Col.column_id = computed_columns.column_id

  left join sys.extended_properties Col_Desc on Col_Desc.major_id=Tab.object_id and Col_Desc.minor_id=Col.Column_id and Col_Desc.class=1

  where Tab.type = 'U' and Tab.Name not like'sys%'

  order by Tab.create_date

  --查询所有视图

  select views.Name as [视图名],Col.Name as [列名] ,Type.name as [数据类型],Col.max_length as [字段长度]

  --,Col_Desc.Value as Col_Description

  from sys.views views

  inner join sys.columns Col on views.object_id = Col.object_id

  inner join sys.types Type on Col.system_type_id = Type.system_type_id

  --left join sys.extended_properties Col_Desc

  -- on Col_Desc.major_id=views.object_id and Col_Desc.minor_id=Col.Column_id and Col_Desc.class=1

  order by Create_Date

  --查询外键约束

  select FK_Name as [外键名],Parent_Tab_Name as [外键表],

  [外键列]=stuff((select ','+[Parent_Col_Name] from (

  select FK.name as FK_Name,Parent_Tab.Name as Parent_Tab_Name,Parent_Col.Name as Parent_Col_Name,

  Referenced_Tab.Name as Referenced_Tab_Name,Referenced_Col.Name as Referenced_Col_Name

  from sys.foreign_keys FK

  inner join sys.foreign_key_columns Col on FK.Object_ID = Col.constraint_object_id

  inner join sys.objects Parent_Tab ON Col.parent_object_id = Parent_Tab.Object_ID and Parent_Tab.TYPE = 'U'

  inner join sys.columns Parent_Col on Parent_Tab.Object_ID = Parent_Col.object_id

  and Col.parent_column_id = Parent_Col.column_id

  inner join sys.objects Referenced_Tab ON Col.referenced_object_id = Referenced_Tab.Object_ID and Referenced_Tab.TYPE = 'U'

  inner join sys.columns Referenced_Col on Referenced_Tab.Object_ID = Referenced_Col.object_id

  and Col.referenced_column_id = Referenced_Col.column_id

  )t where FK_Name=tb.FK_Name and Parent_Tab_Name = tb.Parent_Tab_Name and Referenced_Tab_Name = tb.Referenced_Tab_Name  for xml path('')), 1, 1, ''),

  Referenced_Tab_Name as [主键表],

  [主键列]=stuff((select ','+[Referenced_Col_Name] from (

  select FK.name as FK_Name,Parent_Tab.Name as Parent_Tab_Name,Parent_Col.Name as Parent_Col_Name,

  Referenced_Tab.Name as Referenced_Tab_Name,Referenced_Col.Name as Referenced_Col_Name

  from sys.foreign_keys FK

  inner join sys.foreign_key_columns Col on FK.Object_ID = Col.constraint_object_id

  inner join sys.objects Parent_Tab ON Col.parent_object_id = Parent_Tab.Object_ID and Parent_Tab.TYPE = 'U'

  inner join sys.columns Parent_Col on Parent_Tab.Object_ID = Parent_Col.object_id

  and Col.parent_column_id = Parent_Col.column_id

  inner join sys.objects Referenced_Tab ON Col.referenced_object_id = Referenced_Tab.Object_ID and Referenced_Tab.TYPE = 'U'

  inner join sys.columns Referenced_Col on Referenced_Tab.Object_ID = Referenced_Col.object_id

  and Col.referenced_column_id = Referenced_Col.column_id

  )t where FK_Name=tb.FK_Name and Parent_Tab_Name = tb.Parent_Tab_Name and Referenced_Tab_Name = tb.Referenced_Tab_Name  for xml path('')), 1, 1, '')

  --as [外键列]

  from (

  select FK.name as FK_Name,Parent_Tab.Name as Parent_Tab_Name,Parent_Col.Name as Parent_Col_Name,

  Referenced_Tab.Name as Referenced_Tab_Name,Referenced_Col.Name as Referenced_Col_Name

  from sys.foreign_keys FK

  inner join sys.foreign_key_columns Col on FK.Object_ID = Col.constraint_object_id

  inner join sys.objects Parent_Tab ON Col.parent_object_id = Parent_Tab.Object_ID and Parent_Tab.TYPE = 'U'

  inner join sys.columns Parent_Col on Parent_Tab.Object_ID = Parent_Col.object_id

  and Col.parent_column_id = Parent_Col.column_id

  inner join sys.objects Referenced_Tab ON Col.referenced_object_id = Referenced_Tab.Object_ID and Referenced_Tab.TYPE = 'U'

  inner join sys.columns Referenced_Col on Referenced_Tab.Object_ID = Referenced_Col.object_id

  and Col.referenced_column_id = Referenced_Col.column_id

  )tb

  group by FK_Name,Parent_Tab_Name,Referenced_Tab_Name

  --查询所有存储过程

  select Pr_Name as [存储过程], [参数]=stuff((select ','+[Parameter]

  from (

  select Pr.Name as Pr_Name,parameter.name +' ' +Type.Name + ' ('+convert(varchar(32),parameter.max_length)+')' as Parameter

  from sys.procedures Pr left join

  sys.parameters parameter on Pr.object_id = parameter.object_id

  inner join sys.types Type on parameter.system_type_id = Type.system_type_id

  where type = 'P'

  ) t where Pr_Name=tb.Pr_Name for xml path('')), 1, 1, '')

  from (

  select Pr.Name as Pr_Name,parameter.name +' ' +Type.Name + ' ('+convert(varchar(32),parameter.max_length)+')' as Parameter

  from sys.procedures Pr left join

  sys.parameters parameter on Pr.object_id = parameter.object_id

  inner join sys.types Type on parameter.system_type_id = Type.system_type_id

  where type = 'P'

  )tb

  where Pr_Name not like 'sp_%' --and Pr_Name not like 'dt%'

  group by Pr_Name

  order by Pr_Name

  --查询所有触发器

  select triggers.name as [触发器],tables.name as [表名],triggers.is_disabled as [是否禁用],

  triggers.is_instead_of_trigger AS [触发器类型],

  case when triggers.is_instead_of_trigger = 1 then 'INSTEAD OF'

  when triggers.is_instead_of_trigger = 0 then 'AFTER'

  else null

  end as [触发器类型描述]

  from sys.triggers triggers

  inner join sys.tables tables on triggers.parent_id = tables.object_id

  where triggers.type ='TR'

  order by triggers.create_date

  --查询所有索引

  select indexs.Tab_Name as [表名],indexs.Index_Name as [索引名] ,indexs.[Co_Names] as [索引列],

  Ind_Attribute.is_primary_key as [是否主键],Ind_Attribute.is_unique AS [是否唯一键],

  Ind_Attribute.is_disabled AS [是否禁用]

  from (

  select Tab_Name,Index_Name, [Co_Names]=stuff((select ','+[Co_Name] from

  ( select tab.Name as Tab_Name,ind.Name as Index_Name,Col.Name as Co_Name from sys.indexes ind

  inner join sys.tables tab on ind.Object_id = tab.object_id and ind.type in (1,2)/*索引的类型:=堆/1=聚集/2=非聚集/3=XML*/

  inner join sys.index_columns index_columns on tab.object_id = index_columns.object_id and ind.index_id = index_columns.index_id

  inner join sys.columns Col on tab.object_id = Col.object_id and index_columns.column_id = Col.column_id

  ) t where Tab_Name=tb.Tab_Name and Index_Name=tb.Index_Name for xml path('')), 1, 1, '')

  from (

  select tab.Name as Tab_Name,ind.Name as Index_Name,Col.Name as Co_Name from sys.indexes ind

  inner join sys.tables tab on ind.Object_id = tab.object_id and ind.type in (1,2)/*索引的类型:=堆/1=聚集/2=非聚集/3=XML*/

  inner join sys.index_columns index_columns on tab.object_id = index_columns.object_id and ind.index_id = index_columns.index_id

  inner join sys.columns Col on tab.object_id = Col.object_id and index_columns.column_id = Col.column_id

  )tb

  where Tab_Name not like 'sys%'

  group by Tab_Name,Index_Name

  ) indexs inner join sys.indexes Ind_Attribute on indexs.Index_Name = Ind_Attribute.name

  order by indexs.Tab_Name
本文"SqlServer常用对象查询"由远航站长收集整理而来,仅供大家学习与参考使用。更多AG亚游集团尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 广告合作 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(AG亚游集团). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类AG亚游集团.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4
Gary跑男最后录制视频曝光!90度鞠躬告别 进球gif-无解杀招!权健盯人不紧 空霸暴力头槌 索萨:有段时间注意力不集中 相比其他出线队较弱 落实英烈保护法 微博关闭@暴走漫画等16个账号 商贩称城管指定店家买推车价格贵 城管:工作需要 业主在小区车库挖1.5米深地窖 居民拍视频举报 台军一架无人机小学旁坠毁 50名师生被吓傻(图) 武警改革的出与进:领导管理和指挥问题有望化解 这XX是老子的地盘! MVP也只能跪着看他表演 特恩布尔递补选举惨败 外媒:来年大选面临压力 俄3艘战舰驶过英吉利海峡 英军舰直升机全程跟踪 长沙有墓地售价百万可自行设计 有陵园主打江景墓
女子家暴丈夫十多年 曾开车把丈夫撞成粉碎性骨折 凭借绝密配方可口可乐火了132年,现在它“换药”了 两岸一旦开打能守多久?台叫嚣:解放军根本攻上不来 手机相机真的可以验钞? 专业人士:简直是笑话 高拉特玩游戏秀恩爱两不误 10杀吃鸡了解一下|图 巴西联邦法院:取消卢拉的前任总统礼遇 朱婷是否战绍兴世俱杯待定 郎平承认有不少变数 马尔蒂尼:米兰没找过我任职 未来还不能确定 库里伤退杜兰特砍37分 马刺功亏一篑惜败勇士 湖南荆坪战国墓出土罕见早期玻璃器“蜻蜓眼” 对俄制裁一波三折 美国政府内讧曝光 莫迪访索契见普京 期待加强印俄特惠战略伙伴关系
特斯拉财报前夕 空头已经吹响集结号 黄金期货连续三周下跌 创年内新低 美国一名男子直播时遭枪杀 视频记录遇害经过 高三学生每天画画21个小时 亢奋过度致躁郁症 女出纳沉迷网络赌博 一年半挪用公司6000多万逃跑 虐狗!前国安青年队球员携女友亮相 520见证幸福 南昌赛王蔷横扫晋级仍不满状态 谨慎对待争冠前景 几乎用一节就拿下山东!粤媒赞深圳队太疯狂 李新民委员:把“铁人精神”与国际标准结合起来 前景最好的十大专业 最近开什么店比较好 30万开什么店好 广东水库抓到一只女鬼 AG亚游集团