简体   繁体   English

在SQL Server数据库的所有表中查找特定类型的所有列

[英]Find all columns of a certain type in all tables in a SQL Server database

How can I find all columns of a certain type (for example NTEXT ) in all tables in a SQL Server database? 如何在SQL Server数据库的所有表中查找特定类型的所有列(例如NTEXT )?

I am looking for a SQL query. 我正在寻找一个SQL查询。

You can use following query to return fields 您可以使用以下查询返回字段

SELECT table_name [Table Name], column_name [Column Name]
FROM information_schema.columns where data_type = 'NTEXT'

You're going to need INFORMATION_SCHEMA. 你将需要INFORMATION_SCHEMA。 Try something like: 尝试类似的东西:

SELECT c.* from INFORMATION_SCHEMA.columns c
INNER JOIN INFORMATION_SCHEMA.tables t ON t.table_name = c.table_name
WHERE c.data_type = 'int' AND t.table_type = 'base table'

Also you can try 你也可以试试

SELECT OBJECT_NAME(c.OBJECT_ID) TableName, c.name ColumnName
FROM sys.columns AS c
JOIN sys.types AS t ON c.user_type_id=t.user_type_id
WHERE t.name = 'ntext'
ORDER BY c.OBJECT_ID;
GO

You can use the system view INFORMATION_SCHEMA.COLUMNS . 您可以使用系统视图INFORMATION_SCHEMA.COLUMNS The data_type column has what you're looking for. data_type列包含您要查找的内容。

I did use the following Statement to find all tables that could possibly hold binary-data/files. 我确实使用以下语句来查找可能包含二进制数据/文件的所有表。

SELECT 
    table_name 
FROM 
    INFORMATION_SCHEMA.TABLES T 
WHERE 
    T.TABLE_CATALOG = 'MyDatabase' AND 
    EXISTS ( 
        SELECT * 
        FROM INFORMATION_SCHEMA.COLUMNS C 
        WHERE 
            C.TABLE_CATALOG = T.TABLE_CATALOG AND 
            C.TABLE_SCHEMA = T.TABLE_SCHEMA AND 
            C.TABLE_NAME = T.TABLE_NAME AND 
            ( C.DATA_TYPE  = 'binary' OR
             C.DATA_TYPE  = 'varbinary' OR 
            C.DATA_TYPE  = 'text' OR
            C.DATA_TYPE  = 'ntext' OR
            C.DATA_TYPE  = 'image' )
            )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在SQL Server数据库的所有表中查找nvarchar(max)类型的所有列 - Find all columns of type nvarchar(max) in all tables in a SQL Server database 如何在SQL Server的所有表和列中查找特定值 - How do I find a certain value across all tables and columns in SQL Server SQL Server 2008:查找包含具有指定名称的列的所有表 - SQL Server 2008: find all tables containing columns with specified name 知道SQL服务器中所有数据库表之间的关系类型吗? - Know the type of relationships between all the tables of database in SQL Server? 如何显示数据库中所有架构的所有表中的所有列(及其类型)? - How to display all the columns (and their type) in all tables of all schemas in a database? 一个数据库中所有表的SQL计数列 - SQL count columns of all tables in one database 在 SQL Developer 中,如何查找数据库中所有表的所有列的不同值? - In SQL Developer, how to find distinct values of all columns of all tables in a database? 列出SQL Server表中的所有列 - Listing all columns in SQL Server tables 如何在SQL Server数据库的所有表的所有列中搜索特定的字符串? - How to search for a specific string in all columns within all tables of a SQL Server database? 通过搜索 SQL Server 中的所有表来查找字符串 - Find a string by searching all tables in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM