How to determine the number of "Column Name" in a table? - sql

I have a table tblEmployeeInfowhich has atleast a 100+ column name.
I want to know how many column name are in that table. Is that possible?
NOTE:
tbleEmployeeInfo has no data inside yet.

I would recommend using the INFORMATION_SCHEMA views. You can see all the columns and their types by doing:
select c.*
from INFORMATION_SCHEMA.COLUMNS c
where table_name = 'tbleEmployeeInfo';
(You might want to include the table_schema as well.)
To get the count, just use COUNT(*):
select count(*)
from INFORMATION_SCHEMA.COLUMNS c
where table_name = 'tbleEmployeeInfo';

SELECT COUNT(*)
FROM sys.columns
WHERE object_id = object_id('tblEmployeeInfo')

Related

SQL Match table and multiple columns

I wanted to know a Query where in which i want to locate a specific table that contains two or more columns:
I tried:
SELECT *
FROM DB
WHERE TableName = 'TableName'
AND ColumName in('column1' , 'column2')
But this query will look if any of those columns are there, but i want it to return only if all of them are a match.
I hope this questions makes sense.
This should work for you in MySQL:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('column1','column2')
AND TABLE_SCHEMA='your_database'
GROUP BY table_name
HAVING COUNT(COLUMN_NAME) =2;
The operator IN is a concatenation of OR. However, there's no way to creare a short concatenation of AND as well.
See this question.

Converting one to many relation into a json column in PostgreSQL

I'm trying to query two information_schema tables in PostgreSQL - tables and columns in order to get the following result:
table_name - columns_as_json_array
Sort of converting this one to many relation into a json array column.
I tried a lot of different methods and came up with somethings like this:
SELECT t.table_name, c.json_columns
FROM information_schema.TABLES t
LEFT JOIN LATERAL(
SELECT table_name, json_agg(row_to_json(tbc)) AS json_columns
FROM information_schema.COLUMNS tbc
WHERE t.table_name = tbc.table_name
GROUP BY table_name
) as c ON TRUE;
This results a list of table_names but the json_columns always contains all of the columns available instead of the columns of that certain table.
Any ideas?
I don't really see the point for a lateral join here. As far as concerns, you can get the expected results by aggregating information_schema.columns:
select table_name, json_agg(row_to_json(c)) json_columns
from information_schema.columns c
group by table_name
order by table_name

Query to find column not showing anything?

I'm trying to find a table in a schema with a specific column name. So I used the following script, but it doesn't return anything:
select a.table_name, column_name,DATA_TYPE,DATA_LENGTH
from all_tab_columns a,USER_ALL_TABLES u
where a.TABLE_NAME=u.TABLE_NAME
and column_name like '%LATLONG%'
order by DATA_LENGTH desc;
On the other hand, a SELECT of table LATLONG_DETAIL will display a column called LATLONG_TYPE.
So why isn't the query displaying this table in its result?
All these queries are being run in the schema where table LATLONG_DETAIL resides.
Thanks.
You say you own the table LATLONG_DETAIL. The only other thing I can think of why your query isn't returning anything is that the column name is not in upper case. Does this query return anything?
SELECT a.table_name, column_name,DATA_TYPE,DATA_LENGTH
FROM all_tables u JOIN all_tab_columns a
ON u.table_name = a.table_name
AND u.owner = a.owner
WHERE UPPER(column_name) LIKE '%LATLONG%';

Find table_name that contains two known column names

I want to find the tables that contain both of the two columns together in one table. I tried this:
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('CurrencyName', 'CurrencyKey');
This query generates all the tables that has either one of the CurrencyName or CurrencyKey column.
But I want the table that has both of these columns together.
Please shoot some ideas.
Thanks!
You are close. You want to use group by and then validate that you have two matches using a having clause:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('CurrencyName', 'CurrencyKey')
GROUP BY TABLE_NAME
HAVING COUNT(*) = 2;
Note: To be sure you have the right table, you should use TABLE_SCHEMA as well in the query.

What is the Select statement to return the column names in a table

Is there any select statement to return the list of columns in the table?
The INFORMATION_SCHEMA.COLUMNS view will provide the column names for a particular table name.
SELECT Column_Name + ', '
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Table_Name'
There are several other views as well as the one above which you may find useful. These INFORMATION_SCHEMA views provide information on the schema of your database.
Select top 10 * from Information_Schema.tables
Select top 10 * from Information_Schema.views
Select top 10 * from Information_Schema.routines
Select top 10 * from Information_Schema.parameters
Paul's answer is right for mysql. ON EDIT: and sql server too, apparently. Arrgh. Sorry Paul.
For sql server, you want sys.syscolumns, very similarly to this answer:
How do I look at column metadata in Sybase?
sp_help TableName
Will give you all columns, plus lots of other information.
You can also get column data in SqlServer 2005 using
SELECT column_name 'Column Name',
data_type 'Data Type'
FROM information_schema.columns
WHERE table_name = 'table name'
Srinivas Dontula.
sdonthula#live.com