Here is the simple sql query that will return all tables that have the following column name, ‘userid’ or ‘user_id’.
SELECT t.name
FROM sys.tables t
INNER JOIN sys.columns c ON t.object_id = c.object_id
WHERE c.name LIKE 'userid' OR c.name LIKE 'user_id'
ORDER BY name
However, what I would like to do here is to get all the tables that don’t contain ‘userid’ column and ‘user_id’ column. How should I query this?
P.S. Using NOT LIKE will still bring back the table that contains ‘userid’ or ‘user_id’ column, if the table has some other different columns.
Thanks very much.
You need “tables where no column userid/user_id exists”
Edit:
You can also use your
originalquery in an EXCEPT clauseThese two queries should give identical plans and performance