Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all tables without a certain column name

Tags:

sql

sql-server

So, I've seen lots of questions about finding all tables with a specific column name. However, I'm trying to find all tables WITHOUT a specific column. (In this case, EndDate). Is there a more elegant solution than just finding all the tables with that column, and comparing it to the list of all tables?

like image 923
Colin DeClue Avatar asked Aug 30 '11 15:08

Colin DeClue


People also ask

How do I find a particular column name in all tables in MySQL?

How to list all tables that contain a specific column name in MySQL? You want to look for tables using the name of columns in them. SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA. COLUMNS WHERE COLUMN_NAME IN('column1', 'column2') AND TABLE_SCHEMA = 'schema_name';

How do I find the columns from all tables in a database?

USE YourDatabseName GO SELECT t.name AS table_name, SCHEMA_NAME(schema_id) AS schema_name, c.name AS column_name FROM sys. tables AS t INNER JOIN sys. columns c ON t. OBJECT_ID = c.


1 Answers

Try the following, It's standard SQL (and will work for almost every platform)

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
EXCEPT
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'EndDate'

Just as you suggested, you can't really get anything that's simpler than this.

like image 193
Michael J Swart Avatar answered Oct 04 '22 20:10

Michael J Swart