How do I find table column in different databases? - sql

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>%'

Related

SQL Server - Search for a column across all database tables

I would like to execute a SQL query to find all tables within a database that does not have a particular column?
E.g. I would like a list of tables that do not have a column called 'BranchID'.
Thank you for your attention.
Information Schema Views contain metadata about the database. For more information, go here
Using these views you can do something like this...
SELECT
t.TABLE_NAME
FROM
INFORMATION_SCHEMA.TABLES AS t
WHERE NOT EXISTS
(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS AS c WHERE c.TABLE_NAME = t.TABLE_NAME AND c.COLUMN_NAME = 'BranchID')
select table_name
from INFORMATION_SCHEMA.COLUMNS
except
select table_name
from INFORMATION_SCHEMA.COLUMNS
where column_name = 'BranchID'
(from memory, I might have the column names wrong but should get you most of the way there :) )
This should work.
SELECT table_name
FROM INFORMATION_SCHEMA.columns c
WHERE NOT EXISTS (
SELECT table_name
FROM INFORMATION_SCHEMA.columns cc
WHERE cc.table_name = c.table_name
AND cc.column_name = 'Branch_id'
)
First of all, we select the table_name from Information_Schema.Columns where the table_name is NOT in a query that selects tables WITH the Branch_ID Column
You can use the INFORMATION_SCHEMA.COLUMNS table and INFORMATION_SCHEMA.TABLES table to get the information you want using normal SQL Queries.
In your case you would use a subquery:
select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_NAME NOT IN (select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME='BranchID')
I have created a re-usable procedure that could help you.
CREATE PROC SP_UTIL_findcolumns
#SearchString As varchar(250) = NULL
AS
BEGIN
SELECT
t.TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES AS t
WHERE NOT EXISTS
(SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS AS c
WHERE c.TABLE_NAME = t.TABLE_NAME
AND c.COLUMN_NAME = #SearchString )
END

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'

find sql table name with a particular column

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

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%');