This article explores various ways to search for database objects in SQL database such as tables, stored procedures, functions, and views.

本文探讨了在SQL数据库中搜索数据库对象(例如表,存储过程,函数和视图)的各种方法。

介绍 (Introduction)

SQL Server has many database objects such as table, view, stored procedure, function, constraints, rule, Synonym, triggers. You being a developer or database administrator might work with thousands of databases. Although you should use a proper naming convention for database objects, it is a difficult task to remember the object names. Let’s consider a single production instance that consists of many databases. You want to look for a specific database object but not sure it exists in which database. It is not possible to explore each database and view the object.

SQL Server具有许多数据库对象,例如表,视图,存储过程,函数,约束,规则,同义词,触发器。 您作为开发人员或数据库管理员可能会处理数千个数据库。 尽管您应该为数据库对象使用正确的命名约定,但是记住对象名称是一项艰巨的任务。 让我们考虑一个由许多数据库组成的生产实例。 您要查找特定的数据库对象,但不确定该对象在哪个数据库中。 无法浏览每个数据库并查看对象。

In this article, we explore various ways to search for database objects in the SQL database.

在本文中,我们探索了在SQL数据库中搜索数据库对象的各种方法。

使用sys.objects系统目录视图 (Use sys.objects system catalog view)

We can use system catalog view sys.objects to view all objects in a SQL database. It has a column type that contains the object category. For example, if we want to search only for the user-defined table, we use ‘U’ value for the type column.

我们可以使用系统目录视图sys.objects来查看SQL数据库中的所有对象。 它具有包含对象类别的列类型。 例如,如果我们只想搜索用户定义的表,则对类型列使用“ U”值。

I filter records for the useful columns. You can get all columns using the select * statement.

我过滤了有用列的记录。 您可以使用select *语句获取所有列。

USE [AdventureWorks];
GO
SELECT name AS [Name], SCHEMA_NAME(schema_id) AS schema_name, type_desc, create_date, modify_date
FROM sys.objects
WHERE type ='u'

Similarly, we use the value ‘P’ for the stored procedure.

同样,对于存储过程,我们使用值“ P”

USE [AdventureWorks];
GO
SELECT name AS [Name], SCHEMA_NAME(schema_id) AS schema_name, type_desc, create_date, modify_date
FROM sys.objects
WHERE type ='p'

You can also search for objects modified in ‘N’ number of days. In the below query, we try to find output objects changed in the last 60 days in the sample database [adventureWorks].

您还可以搜索在'N'天后修改的对象。 在下面的查询中,我们尝试在示例数据库[adventureWorks]中查找最近60天内更改的输出对象。

USE [AdventureWorks];
GO
SELECT name AS [Name], SCHEMA_NAME(schema_id) AS schema_name, type_desc, create_date, modify_date
FROM sys.objects
WHERE modify_date > GETDATE() - 60
ORDER BY modify_date;
GO

You can refer to Microsoft docs for different values for the Type column in the sys.objects.

您可以针对sys.objects中“类型”列的不同值参考Microsoft文档 。

使用系统信息架构视图 (Use System Information Schema views)

We can also use information schema views to search for the specific SQL database objects. For example, we use information_schema.Tables to find out specific tables in the SQL database.

我们还可以使用信息架构视图来搜索特定SQL数据库对象。 例如,我们使用information_schema.Tables查找SQL数据库中的特定表。

In the script below, we search for the [Demotable].

在下面的脚本中,我们搜索[Demotable]。

SELECT *
FROM information_schema.Tables
WHERE [Table_Name]='demotable'

We can use this schema view to find database view as well if we run above query without a WHERE clause, it returns both SQL tables and views.

如果我们在没有WHERE子句的情况下运行上述查询,则也可以使用该架构视图查找数据库视图,它会返回SQL表和视图。

Similarly, we can use information_schema.CHECK_CONSTRAINTS to find out check constraints in the current database.

同样,我们可以使用information_schema.CHECK_CONSTRAINTS在当前数据库中查找检查约束。

SELECT *
FROM information_schema.CHECK_CONSTRAINTS

We can use information_schema.SCHEMATA to search for schema and their owners using the below query.

我们可以使用information_schema.SCHEMATA通过以下查询搜索模式及其所有者。

SELECT catalog_name AS DBName, Schema_name, schema_owner
FROM information_schema.SCHEMATA;

You can check the complete list of information schemas using the Microsoft docs.

您可以使用Microsoft文档检查信息架构的完整列表。

使用SSMS对象资源管理器搜索对象的详细信息 (Search object using SSMS object explorer details)

We can use SSMS in-built object search functionality to find out specific objects across all online databases in SQL instance.

我们可以使用SSMS内置对象搜索功能在SQL实例中的所有在线数据库中查找特定对象。

Navigate to View-> Object Explorer Details in SSMS. You can use a keyboard shortcut F7 to open it.

导航到SSMS中的“查看”->“对象资源管理器详细信息”。 您可以使用键盘快捷键F7打开它。

It opens the following screen and shows the various folders – Databases, Security, Server objects, Replication, PolyBase, Always on High Availability. You can also see the search box, as highlighted below.

它打开以下屏幕,并显示各种文件夹-数据库,安全性,服务器对象,复制,PolyBase,始终处于高可用性状态。 您还可以看到搜索框,如下所示。

在单个SQL数据库中搜索对象 (Search objects in a single SQL database)

Here, you can either search the object in a specific database. For that, you can browse the database folder and select the required database.

在这里,您可以在特定数据库中搜索对象。 为此,您可以浏览数据库文件夹并选择所需的数据库。

Now, enter the object name in the search box and press enter.

现在,在搜索框中输入对象名称,然后按Enter。

It searches the object and gives you a result, as shown below. You also get the path in a PowerShell output format.

它搜索对象并为您提供结果,如下所示。 您还可以使用PowerShell输出格式获取路径。

所有在线SQL数据库中的搜索对象 (Search object in all online SQL databases)

You can search for objects in all databases in the connected instance using this object explorer search. On the home page of the object explorer, enter the object name and search.

您可以使用此对象资源管理器搜索在连接的实例中的所有数据库中搜索对象。 在对象资源管理器的主页上,输入对象名称并搜索。

In the result below, you see that a specified object exists in multiple databases.

在下面的结果中,您看到一个指定的对象存在于多个数据库中。

You can browse to the specified object in the database using the object explorer. Click on the particular object and navigate to Synchronize.

您可以使用对象资源管理器浏览到数据库中的指定对象。 单击特定对象,然后导航到Synchronize

It takes you to the object, as shown below.

它将带您到对象,如下所示。

使用SQL Server Management Studio过滤SQL数据库中的对象 (Filter objects in SQL databases using SQL Server Management Studio)

Suppose you know the database in which the object exists. You can filter objects in SSMS to show only relevant objects to you. For example, let’s say we need to search only tables with [HumanResources] schema in the [AdventureWorks] database.

假设您知道对象所在的数据库。 您可以过滤SSMS中的对象以仅向您显示相关对象。 例如,假设我们只需要搜索[AdventureWorks]数据库中具有[HumanResources]模式的表。

To filter the tables, expand [AdventureWorks] database, right-click on the tables and click on Filter settings.

要过滤表,请展开[AdventureWorks]数据库,右键单击表,然后单击“过滤器设置”。

It opens the following filter setting page. You have multiple filter options. You need to specify one or more filter conditions. I specify schema [HumanResources] as shown below:

它将打开以下过滤器设置页面。 您有多个过滤器选项。 您需要指定一个或多个过滤条件。 我指定了架构[HumanResources],如下所示:

Click Ok, and it filters the results as per the specified condition. You can use equal, not equal and contains an operator in the filter.

单击“确定”,它会根据指定条件过滤结果。 您可以使用相等,不相等,并且在过滤器中包含一个运算符。

Once it applies filters, it changes the folder name as well from tables to tables (filtered). It helps you to recognize that you are viewing filtered objects in SSMS.

一旦应用了过滤器,它也将文件夹名称从表更改为表(已过滤)。 它可以帮助您认识到您正在查看SSMS中的筛选对象。

We can remove or modify the filter to go back to the original states in the SSMS object browser. Go back to tables (filtered) and select the options – Remove Filter to clear the filter or Filter Settings to view filter, change it, if required.

我们可以删除或修改过滤器,以返回到SSMS对象浏览器中的原始状态。 返回表(已过滤),然后选择选项–“删除过滤器”以清除过滤器,或选择“过滤器设置”以查看过滤器,必要时进行更改。

在SSMS中使用ApexSQL Search搜索SQL数据库对象 (Use ApexSQL Search in SSMS to search for SQL database objects)

ApexSQL gives you a FREE tool ApexSQL Search to install in SSMS and Visual Studio for object search.

ApexSQL为您提供了免费的ApexSQL Search工具,可将其安装在SSMS和Visual Studio中以进行对象搜索。

Download and install it in your system to integrate with SSMS or Visual Studio. Once installed, Launch SSMS, Connect to SQL instance and navigate to ApexSQL Search -> Object search.

将其下载并安装到系统中,以与SSMS或Visual Studio集成。 安装后,启动SSMS,连接到SQL实例并导航到ApexSQL Search-> Object search。

It opens the object search windows.

它打开对象搜索窗口。

In this window, you can do the following configurations:

在此窗口中,可以进行以下配置:

  • Search text: Enter the keyword you wish to search 搜索文字:输入您要搜索的关键字
  • Server: It is the SQL instance you connected 服务器 :这是您连接SQL实例
  • Database: Here, you can select a single database, multiple databases or all databases 数据库 :在这里,您可以选择一个数据库,多个数据库或所有数据库
  • Object type: By default, it searches in all the objects. You can expand object types and select the specific objects if we want a specific search result. For example, if we want to search the object in the table and stored procedure, just select the required options 对象类型 :默认情况下,它将搜索所有对象。 如果我们需要特定的搜索结果,则可以扩展对象类型并选择特定的对象。 例如,如果我们要搜索表和存储过程中的对象,只需选择所需的选项

Let’s search for the objects containing employee keyword, and you get the detailed results.

让我们搜索包含employee关键字的对象,您将获得详细的结果。

Let’s search for specified object names in the stored procedures and user tables. It gives you the flexibility to view the object definition as well as search results.

让我们在存储过程和用户​​表中搜索指定的对象名称。 它使您可以灵活地查看对象定义以及搜索结果。

You can perform another level of object filter using this ApexSQL Search. Suppose for employee keyword you get 100 results. Now, you want to filter those 100 results as per below:

您可以使用此ApexSQL搜索执行另一级的对象筛选。 假设对于employee关键字,您得到100个结果。 现在,您要按照以下条件过滤这100个结果:

  • An object should belong to schema [HumanResources] 对象应属于架构[HumanResources]
  • Its name should contain a login word 其名称应包含一个登录词

To set these filters, click on the row below the column name, select the comparison operator and your condition.

要设置这些过滤器,请单击列名称下方的行,然后选择比较运算符和您的条件。

It immediately filters the results, and you get an object as per your requirement.

它立即过滤结果,并根据您的要求获得一个对象。

Right-click on the object, Navigate to object explorer node.

右键单击对象, 导航到对象资源管理器节点

It takes you to a specific object in the SSMS.

它带您到SSMS中的特定对象。

By default, it does not perform an exact search for the keyword you specified. In case, we want an exact search for the object name specified so we can put a check on – Exact match as shown below.

默认情况下,它不会对您指定的关键字执行完全搜索。 如果需要精确搜索指定的对象名称,则可以选中– 完全匹配 ,如下所示。

ApexSQL Search is an excellent tool to search for specific objects in all databases of SQL Server. It is integrated well with SSMS so you can easily use it without launching any external program.

ApexSQL Search是在SQL Server所有数据库中搜索特定对象的出色工具。 它与SSMS集成良好,因此您无需启动任何外部程序即可轻松使用它。

结论 (Conclusion)

In this article, we explored various ways to search for SQL database objects in SQL Server. We can use T-SQL, object explorer search, SSMS filter, as well as third-party tools such as ApexSQL Search for the same. You can choose the appropriate tool and search for objects.

在本文中,我们探索了在SQL Server中搜索SQL数据库对象的各种方法。 我们可以使用T-SQL,对象资源管理器搜索,SSMS筛选器以及ApexSQL Search等第三方工具进行搜索。 您可以选择适当的工具并搜索对象。

翻译自: https://www.sqlshack.com/different-ways-to-search-for-objects-in-sql-databases/

在SQL数据库中搜索对象的不同方法相关推荐

  1. SQL数据库中临时表、临时变量和WITH AS关键词创建“临时表”的区别

    原文链接:https://www.cnblogs.com/zhaowei303/articles/4204805.html SQL数据库中数据处理时,有时候需要建立临时表,将查询后的结果集放到临时表中 ...

  2. azure云数据库_在Azure SQL数据库中配置电子邮件通知

    azure云数据库 In this article, we will review how to configure email notifications in the Azure SQL sing ...

  3. 如何将图片存进SQL数据库中以及从数据库读取照片(解决办法)

    目录 1.选择本地图片并存进数据库 2.Save()方法 3.从数据库读取照片 最近项目要需要上传与下载图片,想了想如何存储图片,可以用本地路径与用二进制数据存储到数据库.最后权衡利弊决定将图片存进数 ...

  4. 无法为数据库中的对象分配空间,因为'PRIMARY'文件组已满

    用SQL Server2012,做数据保存时出错,错误信息:无法为数据库'***'中的对象'***'分配空间,因为'PRIMARY'文件组已满.请删除不需要的文件.删除文件组中的对象.将其他文件添加到 ...

  5. web项目实践1-iwebshop项目-页面初始化数据与sql数据库中的数据对比

    当拿到项目后首先应该根据页面初始化数据与SQL数据库中的查询数据进行比对,进行初始化界面测试 初始化页面数据加载分析 1.查询首页的顶级分类 select * from iwebshop_catego ...

  6. mcgs odbc mysql_MCGS中ACCESS数据库的数据提取到SQL数据库中的方法.pdf

    昆仑通泰 MCGS Access数据库与 MSSQL数据库连接 1 在XP 系统上安装 SQLserver2000 1.1找一张 SQLserver 服务器版光盘,在光盘上找到目录"MSDE ...

  7. 爬取豆瓣TOP250电影的评分、评价人数、短评等信息,并在其保存在sql数据库中。

    爬取目标 爬取豆瓣TOP250电影的评分.评价人数.短评等信息,并在其保存在sql数据库中. 最终实现效果如图: 确定爬取的URL 爬取的网页地址为:https://movie.douban.com/ ...

  8. Android学习笔记——保存数据到SQL数据库中(Saving Data in SQL Databases)

    知识点: 1.使用SQL Helper创建数据库 2.数据的增删查改(PRDU:Put.Read.Delete.Update) 背景知识: 上篇文章学习了保存文件,今天学习的是保存数据到SQL数据库中 ...

  9. azure云数据库_在Azure SQL数据库中保护数据的五种方法

    azure云数据库 When storing data in the cloud the main concern companies generally have is whether or not ...

最新文章

  1. Spring和Mybatis整合,配置文件
  2. oracle表设置主键自增长,笔记:oracle表设置主键自增长
  3. 使用JFace Viewer延迟获取模型元素
  4. Mac配置炫酷终端以及直接预览Markdown和各类代码
  5. 让所有IE支持HTML5
  6. wireshark 抓包分析 TCPIP协议的握手
  7. 远程桌面——TeamViewer中“显示黑屏”的功能框无法使用
  8. 怎样得到对方的电脑名_吸引力法则让他想念你,让对方主动找你
  9. O(n^2)以及O(nlogn)时间复杂度的排序算法
  10. 巨波公第3子登国公后裔在荆州(巨波公6子的后裔,全部水落石出)
  11. 鼎利软件测试终端刷机,你怎样把鼎利路测设备调试好?
  12. SRTM 航天飞机雷达地形测绘使命
  13. (php毕业设计)基于php健身俱乐部管理系统源码
  14. linux查看更多历史记录,查看更多历史,如何查看浏览历史记录
  15. *p++、*(p++)、(*p)++、*++p、++*p的区别
  16. 柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
  17. android自动登录简书,android 手机号实现登录功能
  18. java kml_当Java遇上KML
  19. 2017广州(国际)演艺设备、智能声光产品技术展览会GETshow会刊(参展商名录)
  20. STC89C52RC/RD中定时器2的使用方法

热门文章

  1. sql string转number_少用 string.Format
  2. [转帖]备忘:CentOS-7 使用systemctl 管理的服务,文件打开数上限1024要改
  3. 数据结构:树套树-替罪羊树套权值线段树
  4. 免费的HTML5版uploadify
  5. vmware vcenter orchestrator configuration提示“用户名密码错误或登录失败超过次数被锁定”...
  6. 条款34:区分接口继承和实现继承(Different between inheritance of interface and inheritance of implemenation)...
  7. SQL Tuning 基础概述05 - Oracle 索引类型及介绍
  8. IOS开发中--点击imageView上的Button没有任何反应
  9. STL六大组件 功能与运用(略写)
  10. monkey测试_adb monkey压力测试检测安卓手机的5大步骤,你知道吗?