find sql table name with a particular column - sql

Is their any other way or sql query to find the database table names with a particular column than shown below,
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'NameID'

In SQL Server, you can query sys.columns.
Something like:
SELECT
t.name
FROM
sys.columns c
inner join
sys.tables t
on
c.object_id = t.object_id
WHERE
c.name = 'NameID'
You might want an additional lookup to resolve the schema name, if you have tables in multiple schemas.

you can run this query
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.OBJECT_ID
WHERE c.name LIKE '%Column%' -- write the column you search here
ORDER BY schema_name, table_name;

For Oracle Database. Use the below query:
select table_name from all_tab_columns where column_name = 'NameID';
If you’ve got DBA privileges, you can try this command instead:
select table_name from dba_tab_columns where column_name = 'NameID';

SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME,
ORDINAL_POSITION as org_pos, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH as CML
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME like '%Here Column Name%'
ORDER BY TABLE_NAME

Related

Search database for table with 2 or more specified column names

I have the following query that I use very frequently to find a table in a database that has a specified column name:
SELECT Table_Name, Column_Name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = 'db' AND COLUMN_NAME = 'col_A'
I'm now trying to find a table that has both of the specified columns in the query (ex: both col_A and col_B). I thought it would have been as simple as just further qualifying the WHERE clause, but that was to no avail. Any tips?
Another way that satisfies the "2 or more" requirement without major modifications:
;WITH input(ColumnName) AS
(
SELECT y FROM (VALUES
/* simply list your columns here: */
(N'col_A'),
(N'col_B')
) AS x(y)
)
SELECT t.name FROM input
INNER JOIN sys.columns AS c ON c.name = input.ColumnName
INNER JOIN sys.tables AS t ON c.[object_id] = t.[object_id]
GROUP BY t.name HAVING COUNT(*) = (SELECT COUNT(*) FROM input);
Example db<>fiddle
And FWIW why I don't use INFORMATION_SCHEMA.
As long as you know the database already, this should work for you:
select t.TABLE_NAME
from INFORMATION_SCHEMA.TABLES t
inner join INFORMATION_SCHEMA.COLUMNS c
on t.TABLE_NAME = c.TABLE_NAME
and c.COLUMN_NAME = 'col_A'
inner join INFORMATION_SCHEMA.COLUMNS c2
on t.TABLE_NAME = c2.TABLE_NAME
and c2.COLUMN_NAME = 'col_B'
If you want all column names and tables names that have both columnname you can do
SELECT Table_Name, Column_Name,TABLE_CATALOG
FROM INFORMATION_SCHEMA.COLUMNS
WHERE EXISTS( SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_CATALOG = 'testdb' AND column_name = 'col_a')
AND EXISTS( SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = 'testdb' AND column_name = 'col_b')

How do I find table column in different databases?

I am trying to find table columns in different tables and different databases.
So far, I do have a query to do that, however it does not tell me in which database that column and table lays in.
Current code:
SELECT
sys.columns.name AS ColumnName,
tables.name AS TableName
FROM
sys.columns
JOIN
sys.tables ON sys.columns.object_id = tables.object_id
WHERE
sys.columns.name LIKE '%COLUMNNAME%'
Does anyone have an idea what I need to add to display the database name as well?
I'd suggest using INFORMATION_SCHEMA.tables or .columns like:
SELECT table_name, table_schema, table_catalog
from INFORMATION_SCHEMA.tables
where Table_name like '%<table>%'
Same with .columns. Just replace table_name with column_name
SELECT column_name, table_schema, table_catalog, *
from INFORMATION_SCHEMA.COLUMNS where column_name like '%<Column>%'

SQL statement to print table names and their column names

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
ORDER BY TABLE_NAME ASC
I am using this code to print the table names of a db. What I want to do is print the table name and the col names in each table. Can I do this by nesting a statement.
This code is being run on a SQL Server in a query window.
I tried this
SELECT COL_NAME
FROM
(SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
ORDER BY TABLE_NAME ASC)
Any ideas?
This should do it:
SELECT C.TABLE_NAME, C.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES T
WHERE T.TABLE_TYPE='BASE TABLE' AND C.TABLE_NAME=T.TABLE_NAME)
ORDER BY C.TABLE_NAME, C.COLUMN_NAME
In Sqlserver 2005 INFORMATION_SCHEMA views was introduced first.
These views are mainly created to get the metadata like table name,
column name, datatype of columns etc. about tables, columns, views,
domains etc.
Each and every database contains these views. If you want to check what's going on behind the scene you can check the logic of these views by doing just sp_helptext. Like
sp_helptext INFORMATION_SCHEMA.COLUMNS
By using above views you can get your desired result. Please check below query.
SELECT T.TABLE_NAME,C.COLUMN_NAME,C.DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS C
INNER JOIN INFORMATION_SCHEMA.TABLES T ON C.TABLE_NAME = T.TABLE_NAME
AND C.TABLE_SCHEMA = T.TABLE_SCHEMA
WHERE T.TABLE_TYPE = 'BASE TABLE'
With Mssql 2008 version try the following query. This itemise column name and the tables they belong to.
SELECT c.name AS 'ColumnName', t.name AS 'TableName'
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name = c.name
ORDER BY TableName, ColumnName;

How to fetch Column Names from Table?

I am trying to fetch column names form Table in Oracle. But I am not getting Column Names.
I used Many query's, And Find may query's in Stack overflow But I didn't get answer.
I used below query's:
1. SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME='TABLE_NAME';
2. SELECT COLUMN_NAME from ALL_TAB_COLUMNS where TABLE_NAME='TABLE_NAME';
But Out Put is
no row selected
What is the problem here. Thank you very much
both of the queries are correct, just the thing which can cause this problem is that ,maybe you did n't write your table name with capital letters you must do something like this:
SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS
where TABLE_NAME = UPPER('TABLE_NAME');
OR
SELECT COLUMN_NAME FROM USER_TAB_COLUMNS
where TABLE_NAME = UPPER('TABLE_NAME');
Try this:
SELECT column_name
FROM all_tab_cols
WHERE upper(table_name) = 'TABLE_NAME'
AND owner = ' || +_db+ || '
AND column_name NOT IN ( 'password', 'version', 'id' )
or
SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS where TABLE_NAME = UPPER('TABLE_NAME');
I hope This query would help yu out
SELECT SCHEMA_NAME (schema_id) + '.' + t.name AS 'Table Name'
FROM sys.tables t
INNER JOIN sys.columns c
ON c.object_id = t.object_id
WHERE c.name like '%hmy%
ORDER BY 'Table Name'

Finding the specific column (field) name from multiple tables in a database

I have large numbers of tables in my database. I am searching for a column named Country, but don't know which table contains that column. Is there a specific query that will help me to find the name of the table containing this column?
Yes, you can use INFORMATION_SCHEMA.COLUMNS
SELECT DISTINCT
TABLE_NAME
FROM
INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'Country'
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.OBJECT_ID
WHERE c.name LIKE '%ColumnName%'
ORDER BY schema_name, table_name;
Replace ColumnName to your actual column name
select distinct table_schema, table_name from information_schema.columns where table_name = 'Country';
You can find such of information in the INFORMATION_SCHEMA.COLUMNS
USE YourDBName;
SELECT DISTINCT Table_Name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE Column_Name = 'Country';
If we want to find a column name with like operator in more than one tables (ex: 3 tables) then go for below query:
select COLUMN_NAME, TABLE_NAME
from ALL_TAB_COLUMNS
where TABLE_NAME in ('SIL_RLS_UPLOAD_TMP','SIL_CUSTOMER_MST','SIL_PERSONAL_CUSTOMER_MST')
and upper(column_name) like upper('%loan%');
If we want to find a column name with like operator in all tables then go for below query:
select *
from all_tab_columns
where upper(column_name) like upper('%card%');