SQL|SQL Server中搜索特定的对象
检索数据库架构信息 - ADO.NET | Microsoft 官方文档
将系统表映射到系统视图 (Transact-sql) - SQL Server | Microsoft 官方文档
一、注释中带某关键字的对象(sys.extended_properties)
主要用到 sys.tables 、sys.columns 、sys.procedures系统对象表以及sys.extended_properties 扩展属性表
--查询列SELECTA.name AS table_name ,B.name AS column_name ,C.value AS column_descriptionFROMsys.tables AINNER JOIN sys.columns B ON B.object_id = A.object_idLEFT JOIN sys.extended_properties C ON C.major_id = B.object_idAND C.minor_id = B.column_id WHERECAST(C.[value] AS VARCHAR(1000)) LIKE '%年假%'; --查询表SELECTA.name AS table_name ,C.value AS column_description FROMsys.tables AINNER JOIN sys.extended_properties C ON C.major_id = A.object_idAND C.minor_id = 0 WHERECAST(C.[value] AS VARCHAR(1000)) LIKE '%请假%'--查询存储过程SELECTA.name AS table_name ,C.value AS column_description FROMsys.procedures AINNER JOIN sys.extended_properties C ON C.major_id = A.object_idAND C.minor_id = 0 WHERECAST(C.[value] AS VARCHAR(1000)) LIKE '%年假%'
二、定义语句中带某关键字的对象(sys.all_sql_modules ) 主要用到 dbo.sysobjects 系统对象表以及sys.all_sql_modules 对象定义语句表
--老方式SELECTDISTINCT b.name, b.xtypeFROMdbo.syscomments a, dbo.sysobjects bWHEREa.id = b.idAND b.xtype = 'p'AND a.text LIKE '%LotMax%'ORDER BY name; --从 2008 开始,新方式SELECTname, type_descFROMsys.all_sql_modules sINNER JOIN sys.all_objects o ON s.object_id = o.object_idWHEREdefinition LIKE '%LotMax%'ORDER BY type_desc, name;
三、查找列名
selectA.name as table_name, B.name as column_namefromsys.tables Ainner join sys.columns B on B.object_id = A.object_idwhereB.name like '%File%' order by A.name, B.name;
完整的列属性:
with indexCTEas ( select ic.column_id, ic.index_column_id, ic.object_idfromZSOtherData.sys.indexes idxinner join ZSOtherData.sys.index_columns ic on idx.index_id = ic.index_id and idx.object_id = ic.object_idwhereidx.object_id = object_id('MouldTestResultDetail') and idx.is_primary_key = 1 )selectcolm.column_id ColumnID, cast(case when indexCTE.column_id is null then 0 else 1 end as bit) IsPrimaryKey, colm.name column_name ,object_definition(colm.default_object_id) AS column_def,systype.name type_name, colm.is_identity is_identity,f.keyno as is_foreignkey, colm.is_nullable , cast(colm.max_length as int) ByteLength ,( case when systype.name = 'nvarchar' and colm.max_length > 0 then colm.max_length / 2when systype.name = 'nchar' and colm.max_length > 0 then colm.max_length / 2when systype.name = 'ntext' and colm.max_length > 0 then colm.max_length / 2 else colm.max_length end ) length ,cast(colm.precision as int) precision, cast(colm.scale as int) scale,colm.is_computed, prop.value RemarkfromZSOtherData.sys.columns colminner join ZSOtherData.sys.types systype on colm.system_type_id = systype.system_type_id and colm.user_type_id = systype.user_type_idleft join ZSOtherData.sys.extended_properties prop on colm.object_id = prop.major_id and colm.column_id = prop.minor_idleft join indexCTE on colm.column_id = indexCTE.column_id and colm.object_id = indexCTE.object_idleft join sysforeignkeys f on f.fkeyid=colm.object_id and f.fkey=colm.column_idwherecolm.object_id = object_id('MouldTestResultDetail')order by colm.column_id;
文章图片
【SQL|SQL Server中搜索特定的对象】到此这篇关于SQL Server搜索特定对象的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- MySQL数据库设计概念及多表查询和事物操作
- 云原生技术新版图——无服务器(Serverless)数据库
- IntersectionObserver功能(Chrome 51功能,知道特定元素何时处于视口内或视口外)
- PostgreSQL(一)|PostgreSQL(一) 编译安装运行
- 如何用C++自己实现mysql数据库的连接池()
- 同步MySQL数据到ES神器mysqlmom介绍
- MySQLmom程序全量同步MySQL表数据到ES
- MySQLmom程序增量同步MySQL数据到ES
- 如何在Symfony 5中使用MySQL通过数组的特定顺序对Doctrine 2查询结果进行排序
- 【Mysql】MVCC介绍及其实现原理