Multiple Methods and Practical Guide for Table Name Search in SQL Server

Nov 19, 2025 · Programming · 10 views · 7.8

Keywords: SQL Server | Table Name Search | INFORMATION_SCHEMA | sys.tables | Database Metadata

Abstract: This article provides a comprehensive exploration of various technical methods for searching table names in SQL Server databases, including the use of INFORMATION_SCHEMA.TABLES view and sys.tables system view. The analysis covers the advantages and disadvantages of different approaches, offers complete code examples with performance comparisons, and extends the discussion to advanced techniques for searching related tables based on field names. Through practical case studies, the article demonstrates how to efficiently implement table name search functionality across different versions of SQL Server, serving as a complete technical reference for database developers and administrators.

Fundamental Principles of Table Name Search

In database management and development, there is often a need to search for table names based on specific patterns. SQL Server provides multiple system views and functions to access database metadata, making table name searching relatively straightforward. Understanding how these system views work is crucial for efficient usage.

Using INFORMATION_SCHEMA.TABLES View

INFORMATION_SCHEMA.TABLES is an information schema view defined by ANSI SQL standards, providing cross-database platform compatibility. This view contains basic information about all tables and views in the database.

The basic search syntax is as follows:

SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_NAME LIKE '%search_pattern%'

Here, TABLE_CATALOG represents the database name, TABLE_SCHEMA indicates the schema name, TABLE_NAME is the table name, and TABLE_TYPE identifies the object type (BASE TABLE or VIEW).

Using sys.tables System View

sys.tables is a SQL Server-specific system view that provides more detailed table information. Compared to INFORMATION_SCHEMA, sys.tables includes more SQL Server-specific attributes.

The recommended usage method:

SELECT name, schema_id, type_desc, create_date 
FROM sys.tables 
WHERE name LIKE '%xxx%' 
AND is_ms_shipped = 0

The is_ms_shipped field is used to filter out system tables, with a value of 0 indicating user-created tables. This is particularly useful when searching for user-defined tables, as it prevents system tables from cluttering the search results.

Method Comparison and Selection Recommendations

The advantage of INFORMATION_SCHEMA.TABLES lies in its cross-platform compatibility. If a project needs to support multiple database systems, this method is recommended. Meanwhile, sys.tables offers richer SQL Server-specific information and is more advantageous in pure SQL Server environments.

In terms of performance, both methods show little difference in most cases, though sys.tables may have a slight advantage in large databases as it directly accesses system catalog views.

Extended Application: Searching Tables Based on Field Names

In practical development, there is often a need to find tables containing specific fields based on field names. This can be achieved by joining multiple system views.

Example code:

SELECT t.name AS table_name, c.name AS column_name 
FROM sys.tables t 
INNER JOIN sys.columns c ON t.object_id = c.object_id 
WHERE c.name LIKE '%field_name%' 
AND t.is_ms_shipped = 0

This method combines sys.tables and sys.columns views, associating them through object_id to accurately find all user tables containing specific fields.

Practical Considerations

When using the LIKE operator, attention must be paid to wildcard usage. % represents any sequence of characters, while _ represents a single character. The design of search patterns directly affects the accuracy and performance of search results.

For large databases, it is recommended to add appropriate filtering conditions, such as schema names and table types, to reduce the search scope and improve query efficiency.

Version Compatibility Considerations

The methods introduced in this article are applicable to SQL Server 2008 R2 and later versions. The structure of system views remains largely consistent across different versions, ensuring backward compatibility of the code.

During actual deployment, it is advisable to verify query statements in a test environment to ensure they work correctly in the specific version of SQL Server being used.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.