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/