How to get the columns count in a given table in SQL? - sql

I want to get the column count in the given table in SQL. I'm using the DBeaver to do the queries.

Is that connected to a postgres database? Try this script see if it gives you the column count you are a looking for. Remove the aggregate function and it'll give you a lot more system related info on that table
SELECT COUNT(Column_Name) AS ColumnCount
FROM information_schema.columns
WHERE table_schema = 'name of schema'
AND table_name = 'table name';

Related

Select the first 20 columns

Is it possible to select the first 20 columns from my table, without naming out each column in the select?
The columns are always ordered in the same way when I do a select, so there must be some underlying order.
The below query forms the SQL for you. It uses the dictionary table all_tab_columns to fetch the column names for the table.
SELECT ' SELECT '
|| REPLACE(LISTAGG(column_name,',') WITHIN GROUP( ORDER BY column_id),',',','
||CHR(10))
|| ' FROM YOUR_TABLE'
FROM all_tab_columns
WHERE owner ='YOUR_SCHEMA_NAME'
AND table_name='YOUR_TABLE_NAME'
AND column_id <= 20;
you can use column index instead of column name like select 0,1,2,.....
There is a table in a SQL Server database called sysColumns that records all the columns in every table. I think it is a SQL standard and should be in Oracle too.
EDIT: thanks to comment from #davegreen100, this table is in Oracle but is named DBA_TAB_COLUMNS.
try running Select * from DBA_TAB_COLUMNS and see what the results are, and work from there.
If it's there (in Oracle), you will eventually end up with something like
Select name from DBA_TAB_COLUMNS
Where id = #tableId -- <--- the id of the table
and colOrder <= 20
Your final SQL will be probably have to be generated dynamically using the output from the above

Query to get columns from system.catalog table and do a select query

I am trying to build a query to fetch columns from the system.CATALOG table and to continue querying based on the resultset. I looked at a few queries but seem to be unable to find anything that satisfies my requirements. I don't have much to show, that I have tried as I don't know, how to approach this.
I am using Apache Phoenix DB. (Any SQL is also OK, as I am interested in learning.)
I have now written the query below, which will fetch me all the column names, that start with A in schema test for table element.
SELECT
COLUMN_NAME
FROM SYSTEM.CATALOG
WHERE TABLE_SCHEM = 'TEST'
AND TABLE_NAME = 'ELEMENT'
AND COLUMN_NAME LIKE 'A%'";
Now I want to use the list of columns names in an UPSERT query from the resultset of above query, to update these columns in the element table's records. So I am stuck here.
try this, it works perfect.
SELECT column_name
FROM system.catalog
WHERE table_name = 'your_table' AND key_seq IS NOT NULL
Example: To get the salt buckets on the table :
select table_name, salt_buckets
from SYSTEM.CATALOG
where salt_buckets is not null and table_name='TABLE_NAME';

SQL query does not return the data in the columns

I am trying to run a query to get all columns contained in the table if the table exist.
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'2000064'))
The query gets executed but does not return anything! It just say "Query executed".
I have tried in several ways: TABLE_NAME = 2000064, TABLE_NAME = '2000064' but nothing is returned.
The table exists, and there are data in it.
What am I doing wrong?
EDIT:
I need to return the data contained in the table if the table exists.
To get information about the column, uou can just run:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'2000064';
(Note: you should include TABLE_SCHEMA as well to find a specific table.)
If the table does not exist, then you will get no rows back. Putting it in an if exists statement, runs the query and would then run whatever is in the then clause. Your question doesn't have the then part of the statement.

Oracle - Use Column Names IN a Query

I know how to get the column_names from a table in oracle sql.
My question is, is it possible to actually use those column_names in a query?
I can get the column names like this:
SELECT column_name FROM user_tab_cols
WHERE table_name = 'MY_TABLE'
But it just returns a list of column names I cannot do anything with.
Is it possible to use those in a query?
I need to query a bunch of columns in a table and need to grab the column names dynamically so they are not hard coded in there...
Any tips?
You can't do it with just raw SQL, you must use PL/SQL.
So you have to use a stored procedure and do something like that :
SELECT 'SELECT '||LISTAGG(COLUMN_NAME,',')||' FROM /* your condition hew */' INTO myQuery FROM USER_TAB_COLS WHERE table_name = 'OFFER';
And then use OPEN ... LOOP to retrieve your data.
It's just a suggestion, and I found it very complex!
you will have to create a dynamic sql statement, and use the EXECUTE IMMEDIATE call on the sql statement you generate.

DESCRIBE in a FROM subquery

Is it possible to have a DESCRIBE table clause inserted as a subquery in FROM of a SELECT clause in MySQL?
Further, is there a way to enforce a WHERE like condition on a DESCRIBE output?
EDIT: Basically, I have a table with a large number of columns and I want to pull out and act upon the particulars of only one column.
you can use INFORMATION_SCHEMA instead as following:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'Database Name'
AND TABLE_NAME = 'Table Name' and any condition you want...;