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

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

Related

Oracle 19c Database generate JSON from rows without passing field names

I am trying to find a way to query relational tables, without prior knowledge about the table, to generate each row returned as a JSON object without having to execute two queries to achieve this outcome.
For example, If I have a 'Table_1' with columns a,b and c. I am seeking a way to write a single query that will return me json representing each row.
{
"a":"value of a",
"b":"value of b",
"c":"value of c"
}
At this time, I have to execute the following query to find the column names:
SELECT INTO LISTAGG(column_name,', ') from ALL_TAB_COLUMNS where TABLE_NAME="Table_1"
I then formulate a second query (on the client) based on the results of the previous query that gives me the COLUMN_NAMES
SELECT JSON_OBJECT(colnames) from Table_1;
I am trying to save 2 trips to the database by combining the two queries but to no avail. Any idea on how to make this work? I have tried so many permutations and combinations.
Tried this: won't work.
begin
SELECT INTO colnames LISTAGG(column_name,', ') from ALL_TAB_COLUMNS where TABLE_NAME='Table_1' and owner ='owner1'
select JSON_OBJECT(colnames) from Table_1 where a LIKE '%';
END;
Another failed attempt:
select JSON_OBJECT(SELECT LISTAGG(column_name,', ') from ALL_TAB_COLUMNS where TABLE_NAME='Table_1' and owner ='owner1') from Table_1 where a LIKE '%'

How to get few column names among all columns in the one table with query in sql server 2008

select COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='Submenu';
The above query gave all column names in Submenu table but I want only three column names of Submenu table.
Is there any way to get column names?
I assumed this is SQL Server, so the below queries might not work on other RDBMS's).
If your question is how to find only the column names you need (presuming you know in advance which ones those are), you would have to do something like this:
SELECT
COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME='Submenu'
AND COLUMN_NAME IN ('Column1', 'Column2', 'Column3')
At this moment, you basically are requesting a list of any column within your table, without any restrictions whatsoever.
Alternatively, if you're looking only for the first three column names, this would work:
SELECT TOP 3
COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME='Submenu'
ORDER BY
ORDERINAL_POSITION
In the latter case, you will have to determine how you want to sort the column names though (either by using something like ORDER BY COLUMN_NAME, in case you want them listed alphabetically, or ORDER BY ORDERINAL_POSITION in case you're trying to get them in the order they appear in the table).
If this is not what you meant, please elaborate on what you are trying to achieve.
SELECT TOP 3 COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Submenu';
As easy as that!

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.

Mysql Query data filled check

I had created the table with 200 columns and i had inserted data
Now i need to check that specific 100 columns in one row are filled or not,how can we check this using mysql query .the primary key is defined .please help me out how to resolve this.
select * from tablename where column1 != null or column2 != null ......
That is a lot of columns so at the risk of being mysql server version specific you can use the information schema to get the column names and then write a SQL procedure or something in your chosen shell / language that iterates over them performing a test.
select distinct COLUMN_NAME as 'Field', IS_NULLABLE from information_schema.columns where TABLE_SCHEMA="YourDatabase" and TABLE_NAME="YourTableName" and TABLE_NAME not like "%view%" escape '!' ;
The example above will tell you the column name as "Field" and tell you if it can hold a NULL. Having the field name may give you a better way of automating a field name specific test.

Get column name for a table

I want a query that returns me name of columns of a table.
I tried following query -
SELECT column_name ColName
FROM all_tab_columns
WHERE table_name ='<TABLENAME>';
but this query is returning duplicate column names.
I do not want to use distinct in my query cause it hampers the performance.
So is there a query which returns columns of a table without any duplicate.
This is the solution for getting column name from table.
select column_name,data_type from cols where Table_name='TEST_TABLE'