Home >Database >Mysql Tutorial >How Can I Find Tables Containing Columns with a Specific Name in SQL Server?

How Can I Find Tables Containing Columns with a Specific Name in SQL Server?

DDD
DDDOriginal
2025-01-20 00:36:08273browse

How Can I Find Tables Containing Columns with a Specific Name in SQL Server?

How to find tables containing specific column names in SQL Server?

Using Transact-SQL query, you can query the table name containing the specified column name.

Lookup table:

To find tables that contain columns with names similar to '%MyName%', use the following query:

<code class="language-sql">SELECT      c.name  AS 'ColumnName'
            ,(SCHEMA_NAME(t.schema_id) + '.' + t.name) AS 'TableName'
FROM        sys.columns c
JOIN        sys.tables  t   ON c.object_id = t.object_id
WHERE       c.name LIKE '%MyName%'
ORDER BY    TableName
            ,ColumnName;</code>

Lookup tables and views:

To find tables and views that contain columns with names similar to '%MyName%', use the following query:

<code class="language-sql">SELECT      COLUMN_NAME AS 'ColumnName'
            ,TABLE_NAME AS  'TableName'
FROM        INFORMATION_SCHEMA.COLUMNS
WHERE       COLUMN_NAME LIKE '%MyName%'
ORDER BY    TableName
            ,ColumnName;</code>

The above is the detailed content of How Can I Find Tables Containing Columns with a Specific Name in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn