In SQL Server how do you query a database to bring back all the tables that have a field of a specific name?
The following query will bring back a unique list of tables where Column_Name is equal to the column you are looking for:
SELECT Table_Name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE Column_Name = 'Desired_Column_Name'
GROUP BY Table_Name
SELECT Table_Name
FROM Information_Schema.Columns
WHERE Column_Name = 'YourFieldName'
I'm old-school:
SELECT DISTINCT object_name(id)
FROM syscolumns
WHERE name = 'FIELDNAME'
Related
Most of the tables in the DB have a certain column 'col_abc'.
How can I find the tables that do not contain this column 'col_abc'.
The query
select table_name
from user_tab_columns
where column_name not in ('COL_ABC')
does not return expected result.
Thanks,
Amit
Here is one way:
select table_name from user_tables
minus
select table_name from user_tab_columns
where column_name = 'ABC';
The Below Query will give the expected results. Please modify <ur_Schema_name> as the required schema name
SELECT * FROM ALL_TABLES at
WHERE at.OWNER = <ur_Schema_name>
AND NOT EXISTS (SELECT 1 FROM ALL_TAB_COLUMNS atc
WHERE atc.TABLE_NAME = at.TABLE_NAME
AND atc.OWNER =at.OWNER
AND atc.COLUMN_NAME='COL_ABC'
AND atc.OWNER=<ur_Schema_name>);
If you only need to look in the current user's schema, this is probably the simplest way to write the query:
select table_name
from user_tables
where table_name not in (
select table_name
from user_tab_columns
where column_name = 'COL_ABC'
)
;
This uses in a crucial way the fact that the column table_name in user_tab_columns is constrained not null.
If you need to look across all schemas, you need to use the dba_ or all_ dictionary views, and look for (owner, table_name) pairs instead of just table_name. Left as further exercise for you.
You could also use this solution using LEFT JOIN. It is a kind of NOT EXISTS clause.
select t.*
from user_tables t
left join (
select table_name
from user_tab_columns
where column_name = 'COL_ABC'
) tc
on (t.table_name = tc.table_name)
where tc.table_name is null
;
I am working with two very wide tables (Table A, Table B) in Snowflake. Both tables should have exact same columns, though not necessarily in the same order. However Table B is missing a few columns. Is there a way to quickly find all the columns that are in Table A but not in Table B? I am not concerned with the Data, just the existence of columns.
Essentially, I am looking for a SQL way to find all columns in Table that are missing in Table B.
I currently use a ETL tool for this. But it is rather cumbersome. It would be nice if this can be done using SQL.
Try the following
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'tableA'
AND column_name NOT IN
(
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'tableB'
);
You can do this with aggregation:
SELECT column_name
FROM information_schema.columns
WHERE table_name IN ('tableA', 'tableB')
GROUP BY column_name
HAVING MIN(table_name) = MAX(table_name) AND MIN(table_name) = 'TableA';
This is a handy structure that can more easily generalize to multiple tables. It does assume that the two tables are on the same server.
Below query also gives missing columns in Table B.
SELECT column_name FROM information_schema.columns WHERE table_name = 'TableA'
MINUS
SELECT column_name FROM information_schema.columns WHERE table_name = 'TableB'
I have one database on my server I want to filter one column name from the whole database where that column is used.
E.g: If column name is "AlternativeID" is exist in 5 tables and than I want the query that find this column name exists in which tables.?
I find the following query to find specific column name from database.
SELECT * FROM sys.columns WHERE name LIKE '%AlternativeID%'
I hope this is make sense to everyone. Any help will appreciated. Thanks!!
Try this:
SELECT c.name AS ColName, t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%AlternativeID%'
OR...
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%AlternativeID%'
Source : Find all tables containing column with specified name
Hope this helps...
Try:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('AlternativeID')
AND TABLE_SCHEMA='YourDatabaseName';
I'm trying to get make a query to return a list of the column names from a query result in postgresql
e.g.
SELECT column_name
FROM (
SELECT table1.*
FROM table1
LEFT JOIN table2
ON table1.id = table2.tbl1_id
)
is this possible
I DO NOT WANT THE COLUMNS FROM A SINGULAR TABLE!!! so please dont tell me to do
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'table1'
I don't know why you would ever need to do this, but it is possible using some of the JSON functions introduced in 9.3.
SELECT json_object_keys(row_to_json(t)) FROM
(SELECT * FROM table1
LEFT JOIN table2 ON table1.id = table2.tbl1_id LIMIT 1) t;
This will give you the name of every column returned for a single row. Without the LIMIT you would get the columns repeated for every row returned. If you wanted to see the values returned as well you can get more complex:
WITH t as
(SELECT * FROM table1
LEFT JOIN table2 ON table1.id = table2.tbl1_id LIMIT 1)
SELECT json_data.key, json_data.value
FROM t, json_each_text(row_to_json(t)) AS json_data;
Both these queries will return all the columns even if they are named the same. If all you want is a list of unique column names, you can utilize hstore:
CREATE EXTENSION hstore; --Create the extension if you need it.
SELECT akeys(hstore(t)) as array_of_columns
FROM
(SELECT * FROM table1
LEFT JOIN table2 ON table1.id = table2.tbl1id LIMIT 1) t;
Maybe there is a less hacky way, but I think this should work:
create table query_column_sample from (your query where 1=2);
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'query_column_sample'
Querying tables returns data. Querying the information schema returns metadata. Column names are metadata.
SELECT 'table1', column_name
FROM information_schema.columns
WHERE table_catalog = 'database_name'
AND table_schema = 'schema_name'
AND table_name = 'table1'
UNION ALL
SELECT 'table2', column_name
FROM information_schema.columns
WHERE table_catalog = 'database_name'
AND table_schema = 'schema_name'
AND table_name = 'table2';
"schema_name" is probably public. But be warned that you can have multiple tables with the same name, as long as they are in different schemas. (You could have "public.table1" and "private.table1".) You can also have multiple tables with the same name in different databases. You need to specify all three values--database name, schema name, table name--to be sure of getting the right data.
If I had to do this in production I'd probably output more data. I'd also probably include the column "ordinal_position", and sort on it. Sorting by "ordinal_position" will order column names as they appear in the current table definition.
SELECT table_catalog
, table_schema
, table_name
, column_name
, ordinal_position
FROM information_schema.columns
WHERE table_catalog = 'sandbox'
AND table_schema = 'public'
AND table_name IN ('table1', 'table2')
ORDER BY table_catalog
, table_schema
, table_name
, ordinal_position;
The only reasonable approach I can come up with is to do something like this. Schema location plays into this, but I'm just posting the basic idea. SQL doesn't have reflection at run time unless you go and hit meta data tables, which is what you seem to be trying to achieve here.
SELECT table1.*
INTO x
FROM table1
LEFT JOIN table2
ON table1.id = table2.tbl1_id LIMIT 0;
select *
from information_schema.columns
where table_Name = 'x';
Also interesting enough while traveling the Internets over lunch I ran into this. Works with PostgreSQL 8.2.0 or higher.
https://github.com/theory/colnames/tree/master/test/sql
I have a large database and would like to select table names that have a certain column name. I have done something like this in MySQL, but can't find any info on SQL Server.
I want to do something like:
select [table]
from [db]
where table [has column 'classtypeid']
How can I do something like this?
Use the ANSI information_schema views, this will also work in MySQL
select table_name
from information_schema.columns
where column_name = 'classtypeid'
Here you go:
SELECT C.TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS AS C
INNER JOIN INFORMATION_SCHEMA.TABLES AS T ON C.TABLE_NAME = T.TABLE_NAME
AND C.TABLE_SCHEMA = T.TABLE_SCHEMA
WHERE C.COLUMN_NAME = 'classtypeid'
AND T.TABLE_TYPE = 'BASE TABLE'
Edit: Note that this will not list views based on any tables with that column. If you only query INFORMATION_SCHEMA.COLUMNS you will also get back views.