How to execute result of a query in PostgreSQL - sql

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

Related

Select data for columns that are only in another table

I have two tables, table1 and table2, that have columns common to both.
The queries that obtain the column names of each table are given below,
Query to get columns from first table:
select column_name from information_schema.columns
where table_schema = 'schema1'
and table_name = 'table1';
Query to get columns from second table:
select column_name from information_schema.columns
where table_schema = 'schema2'
and table_name = 'table2';
I need to select data from table2, only the columns which are also in table1.
I do not have a postrgesql intace at the moment but Dynamic SQL is what you need.
The following query will get you the column names that appear in both table 1 and table 2.
select string_agg(column_name, ',') FROM (
select column_name from information_schema.columns
where table_schema = 'schema1'
and table_name = 'table1'
intersect
select column_name from information_schema.columns
where table_schema = 'schema2'
and table_name = 'table2'
)
And you need to build
EXECUTE 'select ' || select string_agg(column_name, ',') FROM (
select column_name from information_schema.columns
where table_schema = 'schema1'
and table_name = 'table1'
intersect
select column_name from information_schema.columns
where table_schema = 'schema2'
and table_name = 'table2'
) || ' from schema2.table2 '
Sory if there any syntax error as writing from mobile app, you can join both result set to get common data.
select column_name from information_schema.columns T2
JOIN (select column_name from information_schema.columns where table_schema = 'schema1' and table_name = 'table1') T1
ON T2.COLUMN_NAME = T1.COLUMN_NAME Where T2.table_schema = 'schema2' and T2.table_name = 'table2';

How to find all the tables having column with CHAR Datatype and having numeric value in it?

I want to find all the tables which have columns with char data type but have numeric entry in it ,for instance if there is some table X with column VALID_FLAG CHAR(1) , then I have to check if this column has value 0 or 1 in it ?
I have no clue of this ,Please guide .
You can not achieve this using single query. You can write the PL/SQL procedure in oracle which will do the following:
Get the list of tables with columns where datatype is CHAR:
select table_name,COLUMN_NAME,DATA_TYPE from ALL_TAB_COLUMNS where data_type='CHAR'
and OWNER='OWNER_NAME';
Then you can put the check for 1 or 0:
select count(*) from table_name where column_name in ('1','0');
You can take advantage of xmlquery and find the desired output in a single query as following:
WITH DATAA AS(select /*+ MATERIALIZE */ owner, table_name, column_name,
to_number(xmlquery('/ROWSET/ROW/C/text()'
passing xmltype(dbms_xmlgen.getxml(
'select count(1) as c '
|| 'from ' || table_name || ' WHERE ' || COLUMN_NAME || ' IN (''0'',''1'')'))
returning content)) as c
from user_tab_columns
WHERE
DATA_TYPE = 'CHAR')
SELECT * FROM DATAA
WHERE C > 0;
Cheers!!

Oracle Pl/SQL alter table columns

I have created a table2 with SQL query: create table2 as select * from table1
Create table as select ... doesn't retain all column properties from table1 in table2.
Is it possible to generate an SQL file to ALTER table2 column properties (DATA_LENGTH,DATA_PRECISION,DATA_SCALE,DEFAULT_LENGTH,DATA_DEFAULT) with all column properties from table1?
Thanks!
--drop table table2;
create table table1 (
x number(10, 2) default 10,
y varchar2(200) default 'NA'
);
create table table2 as select * from table1;
Let us try for data_default:
create table search_user_tab_columns as
select table_name, column_name, to_lob(DATA_DEFAULT) dd
from user_tab_columns
where table_name = 'TABLE1';
As data_default is a long data in user_tab_columns we have to create an intermediate
search_user_tab_columns (dummy) :-(
select 'ALTER TABLE "' || table_name || '" MODIFY "' || column_name || '" DEFAULT ' || Dd || ';'
from search_user_tab_columns
where table_name = 'TABLE1';
Note:
SELECT DBMS_METADATA.GET_DDL('TABLE','<TABLE_NAME>','<SCHEMA_NAME>') from dual;
This is better way of doing this if creating a table from an existing table . But sometimes you may try this for specific purposes.
If you only need to get the properties.
SELECT DBMS_METADATA.GET_DDL('TABLE','<TABLE_NAME>','<SCHEMA_NAME>') from dual;
try this:
you can see all properties
select column_name as "Name"
, nullable as "Null?"
, concat(concat(concat(data_type,'('),data_length),')') as "Type"
from user_tab_columns
where table_name = 'MY_TABLE';

Oracle: Count non-null fields for each column in a table

I need a query to count the total number of non-null values for each column in a table. Since my table has hundreds of columns I'm looking for a solution that only requires me to input the table name.
Perhaps using the result of:
select COLUMN_NAME from ALL_TAB_COLUMNS where TABLE_NAME='ORDERS';
to get the column names and then a subquery to put counts against each column name? The additional complication is that I only have read-only access to the DB so I can't create any temp tables.
Slightly out of my league with this one so any help is appreciated.
Construct the query in SQL or using a spreadsheet. Then run the query.
For instance, assuming that your column names are simple and don't have special characters:
select replace('select ''[col]'', count([col]) from orders union all ',
'[col]', COLUMN_NAME
) as sql
from ALL_TAB_COLUMNS
where TABLE_NAME = 'ORDERS';
(Of course, this can be adapted for more complex column names, but I'm trying to show the idea.)
Then copy the code, remove the final union all and run it.
You can put this in one string if there are not too many columns:
select listagg(replace('select ''[col]'', count([col]) from orders',
'[col]', COLUMN_NAME
), ' union all '
) within group (order by column_name) as sql
from ALL_TAB_COLUMNS
where TABLE_NAME = 'ORDERS';
You can also use execute immediate using the same query, but that seems like overkill.
If you're happy with the results row-ar rather than column-ar:
SELECT 'SELECT ''dummy'', 0 FROM DUAL' FROM DUAL
UNION ALL
SELECT
' UNION ALL SELECT ''' ||
column_name ||
''', COUNT(' ||
column_name ||
') FROM ' ||
TABLE_NAME
FROM
all_tab_columns
WHERE
table_name = 'ORDERS'
This is an "SQL that writes an SQL" that you can then copy and run to get your answers. Should make a resultset that looks like:
SELECT 'dummy', 0 FROM dual
UNION ALL SELECT 'col1', COUNT(col1) FROM ORDERS
UNION ALL SELECT 'col2', COUNT(col2) FROM ORDERS
...
If you want your results column-ar:
SELECT 'SELECT '
UNION ALL
SELECT
'COUNT(' ||
column_name ||
') as count_' ||
column_name ||
', ' ||
TABLE_NAME
FROM
all_tab_columns
WHERE
table_name = 'ORDERS'
UNION ALL
SELECT 'null as dummy_column FROM ORDERS'
Should make a resultset that looks like:
SELECT
COUNT(col1) as count_col1,
COUNT(col2) as count_col2,
...
null as dummycoll FROM orders
Caveat: I don't have oracle installed anywhere I can test these, it's written from memory and may need some debugging
This will generate the SQL to get the counts in columns and will handle case sensitive column names and column names with non-alpha-numeric characters:
SELECT 'SELECT '
|| LISTAGG(
'COUNT("' || column_name || '") AS "' || column_name || '"',
', '
) WITHIN GROUP ( ORDER BY column_id )
|| ' FROM "' || table_name || '"' AS sql
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = 'ORDERS'
GROUP BY TABLE_NAME;
or, if you have a large number of columns that is generating a string longer than 4000 characters you can use a custom aggregation function to aggregate VARCHAR2s into a CLOB and then do:
SELECT 'SELECT '
|| CLOBAgg( 'COUNT("' || column_name || '") AS "' || column_name || '"' )
|| ' FROM "' || table_name || '"' AS sql
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = 'ORDERS'
GROUP BY TABLE_NAME;
In Oracle 19 (I used similar code in Ora 12, maybe that works too), this works without generating another select to execute:
select * from
(
select table_name, column_name,
to_number( extractvalue( xmltype(dbms_xmlgen.getxml('select count(to_char(substr('||column_name||',1,1))) c from '||table_name)) ,'/ROWSET/ROW/C')) count
from all_tab_columns where owner = user
)
--where table_name = 'MY_TABLE'
;
It will create XML with count, from which it extracts the current count. The substr and to_char functions here are used to extract first character, so it will works with CLOB columns also

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'