How to fetch Column Names from Table? - sql

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'

Related

How to execute result of a query in PostgreSQL

I am trying to create a view by joining two tables. These two tables have a few columns with the same name. Which gives an error
SQL Error [42701]: ERROR: column "column_name" specified more than once
I cannot use column names while creating the view as there are 30+ columns and new columns will be added to both tables over the period of time. Hence, I have to use * to get all the columns.
Now, to eliminate columns which exist in both the table, I went ahead and did this:
SELECT 'SELECT ' || STRING_AGG('u2.' || column_name, ', ') || ' FROM schema_name.table_name u2'
FROM information_schema.columns
WHERE table_name = 'table_name'
AND table_schema = 'schema_name'
AND column_name NOT IN ('column_name');
This gives me the query to select data from schema_name.table_name without the column column_name. Great!!
The problem: How do I execute the result of the above query?
I tried PREPARE statement, it is just executing the above query and not the result of the above query.
Also, creating a temporary table with no column "column_name" isn't a viable solution.
You need to prepare a dynamic query and then EXECUTE it. It would be something like this:
DO
$do$
BEGIN
EXECUTE (
SELECT CONCAT('CREATE VIEW temp_view AS SELECT ',
-- SELECT table1 columns here
(SELECT STRING_AGG('u1.' || column_name, ', ')
FROM information_schema.columns
WHERE table_name = 'table1'
AND table_schema = 'schema_name'
-- AND column_name NOT IN ('column_name') -- not omitting for table1
),
', ',
-- SELECT table2 columns here
(SELECT STRING_AGG('u2.' || column_name, ', ')
FROM information_schema.columns
WHERE table_name = 'table2'
AND table_schema = 'schema_name'
AND column_name NOT IN ('column_name')),
-- Dynamically prepare the FROM and JOIN clauses
' FROM table1 u1 JOIN table2 u2 ON u1.id = u2.table1_id'));
END
$do$;
CHECK DEMO

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

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