I got this SQL query:
select rtrim(extract(xmlagg(xmlelement(e, column_name || ',')),
'/E/text()').getclobval(), ',') from all_tab_columns
where OWNER = 'TESTER' AND TABLE_NAME = 'H4_POSIT';
I using this instead of LISTAGG(column_name, ',') because the result is going to exceed the limit of varchar2 (>4000).
Now I am asking myself whether it is possible to sort the result like LISTAGG does it.
So when having columns FERA, BAUT, CHECK_ID, ... I'd like them to be returned as: BAUT,CHECK_ID,FERA, ...
I am using Oracle Server and my framework doesn't allow me to work with PL/SQL.
XMLAGG supports ordering on its own (see https://docs.oracle.com/database/121/SQLRF/functions251.htm):
SELECT
rtrim(
extract(
xmlagg(
xmlelement(e, column_name || ',') ORDER BY column_name
),
'/E/text()')
.getclobval (),
',')
FROM
all_tab_columns
WHERE
owner = 'TESTER' AND table_name = 'H4_POSIT'
You can use a subquery and simply sort the columns before you pass it to the xml function. A simple solution.
select rtrim(extract(xmlagg(xmlelement(e, column_name || ',')),
'/E/text()').getclobval(), ',') from
(select * from all_tab_columns
where OWNER != 'TESTER' AND TABLE_NAME=upper('H4_POSIT')
order by COLUMN_NAME );
Related
I have selected (from all_tab_columns) some tables and columns of DATE datatype. I would like to know the MAX date for each table. Could you help me out with a dynamic way to do it? Table names and column names could be different depending on my where clause when selecting from all_tab_columns.
Sample data:
WITH
tabs (TABLE_NAME, COLUMN_NAME, DATA_TYPE) AS
(
Select 'A_ZR_6', 'CREATED_DATE', 'DATE' From dual Union All
Select 'A_ZR_8', 'CREATEDDATE', 'DATE' From dual Union All
Select 'A_ZR_2', 'CREATED_DATE', 'DATE' From dual Union All
Select 'A_ZR_4', 'CREATED_DATE', 'DATE' From dual Union All
Select 'A_ZR_9', 'CREATED_DATE', 'DATE' From dual
)
TABLE_NAME
COLUMN_NAME
DATA_TYPE
A_ZR_6
CREATED_DATE
DATE
A_ZR_8
CREATEDDATE
DATE
A_ZR_2
CREATED_DATE
DATE
A_ZR_4
CREATED_DATE
DATE
A_ZR_9
CREATED_DATE
DATE
The expected result should look like here:
TABLE_NAME
MAX_DATE
A_ZR_6
07-NOV-22
A_ZR_8
12-DEC-22
A_ZR_2
03-OCT-22
A_ZR_4
01-NOV-22
A_ZR_9
31-DEC-22
CODE:
select
table_name,
column_name
from
all_tab_columns
where
owner='ABC' and
table_name not like 'V_%' and
lower(column_name) like '%create%' and
lower(column_name) like '%date%'
group by
table_name,
column_name
I believe that the only method that you can use is a PL/SQL program because the name of table in a SQL statement Oracle cannot dynamic:
SET LINE 100;
SET SERVEROUTPUT ON;
DECLARE
CURSOR QCursor is
SELECT
X.table_name,
'SELECT MAX(' || X.column_name || ') from ' || owner || '.' || X.table_name as query
FROM (
SELECT
owner,
table_name,
column_name
FROM
all_tab_columns
WHERE
owner = 'ABC'
AND table_name not like 'V_%'
AND lower(column_name) like '%create%'
AND lower(column_name) like '%date%'
GROUP BY owner,
table_name,
column_name
) X;
--# RIGA DEL CURSORE CONTROLLO
ROW_QCursor QCursor%ROWTYPE;
v_max_date DATE;
BEGIN
DBMS_OUTPUT.ENABLE(1000000);
OPEN ROW_QCursor;
LOOP
FETCH QCursor INTO ROW_QCursor;
EXIT WHEN QCursor%NOTFOUND;
EXECUTE IMMEDIATE ROW_QCursor.query INTO v_max_date;
DBMS_OUTPUT.PUT_LINE('Table_name :' || ROW_QCursor.table_name || ' Max date is : ' || v_max_date);
END LOOP;
CLOSE ROW_QCursor;
END;
/
Alternatively, if the query is manually (es. Sql Developer) you can run this query:
select
X.table_name,
'SELECT MAX(' || X.column_name || ') from ' || owner || '.' || X.table_name as query
from (
select
owner,
table_name,
column_name
from
all_tab_columns
where
owner = 'ABC'
and table_name not like 'V_%'
and lower(column_name) like '%create%'
and lower(column_name) like '%date%'
group by owner,
table_name,
column_name
) X;
and then copy the output and execute manually:
TABLE_ QUERY
------ ----------------------------------------
A_ZR_2 SELECT MAX(CREATED_DATE) from ABC.A_ZR_2
A_ZR_9 SELECT MAX(CREATED_DATE) from ABC.A_ZR_9
A_ZR_8 SELECT MAX(CREATED_DATE) from ABC.A_ZR_8
A_ZR_6 SELECT MAX(CREATED_DATE) from ABC.A_ZR_6
A_ZR_4 SELECT MAX(CREATED_DATE) from ABC.A_ZR_4
I hope I was helpful.
Thank you.
I have a requirement to pivot the columns of a particular table specified from a user.
the problem is the number of columns from each table to pivot is dynamic.
so the code below gets the name of the columns from the table.
SELECT DISTINCT
LISTAGG('''' || column_name || '''', ',')
WITHIN GROUP (ORDER BY column_name) AS temp_in_statement
FROM (SELECT DISTINCT column_name FROM all_tab_columns WHERE table_name = 'DIM_XYZ')
the code above returns the columns in the following format:
col1, col2
I have to use a pivot for this requirement and plug the code above in the code below in order to pivot the columns.
SELECT * FROM
(
SELECT table_name, column_name
FROM ALL_TAB_COLUMNS
WHERE
table_name = 'DIM_XYZ'
)
PIVOT
(
MIN(column_name)
FOR column_name IN (
-- values added manually
'col1','col2'
-- values added manually
)
)
ORDER BY table_name;
The code works fine in this case but when replacing 'col1','col2' by the select statement to retrieve columns names the system throws the following error:
ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
Error at Line: 39 Column: 40
CODE:
SELECT * FROM
(
SELECT table_name, column_name
FROM ALL_TAB_COLUMNS
WHERE
table_name = 'DIM_XYZ'
)
PIVOT
(
MIN(column_name)
FOR column_name IN (
--code below does not work when plugged in the statement above
SELECT DISTINCT
LISTAGG('''' || column_name || '''', ',')
WITHIN GROUP (ORDER BY column_name) AS temp_in_statement
FROM (SELECT DISTINCT column_name FROM all_tab_columns WHERE table_name = 'DIM_XYZ')
--code above does not work
)
)
ORDER BY table_name;
----------------
do you guys have any idea how to solve this problem?
You cannot directly add dynamic expressions as an input to Pivot table,
You can try something like this where we retrieve all the columns of the table in a variable via a PL/SQL Block and then pass it in a way to the expected by the Oracle Pivot Table Functionality.
SET serveroutput ON;
DECLARE
sqlquery VARCHAR(32767);
cols VARCHAR2(32767);
BEGIN
SELECT listagg('''' || column_name || '''', ',') within
GROUP(
ORDER BY column_name)
INTO cols
FROM
(SELECT DISTINCT column_name
FROM all_tab_columns
WHERE TABLE_NAME = 'TABLE_NAME')
;
sqlquery := '
SELECT * FROM
(
SELECT table_name, column_name
FROM ALL_TAB_COLUMNS
WHERE
table_name = ''TABLE_NAME''
)
PIVOT
(
MIN(column_name)
FOR column_name IN (
''||cols||''
)
)
ORDER BY table_name';
DBMS_OUTPUT.PUT_LINE(sqlquery);
EXECUTE IMMEDIATE sqlquery;
END;
/
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
Seems like this is impossible, but I'm so close - maybe someone can take me the last step...
I have a bunch of dynamic code and I don't always know the tables and columns I'm going to be dealing with, but I do know that VARCHAR2 columns with data_lengths of 2000 result in errors. I'd love to be able to identify these 'bad' columns dynamically, and remove them from my results in 1 shot.
This code:
SELECT LISTAGG(probs.column_name, ', ')
WITHIN GROUP (ORDER BY column_name) FROM
(select 1 grp, column_name
from all_tab_columns
where TABLE_NAME = 'MYTABLE' AND
DATA_TYPE <> 'VARCHAR2' AND
DATA_LENGTH < 2000
) probs
GROUP BY GRP
Gives me a nice comma, separated list of all of my acceptable column names like this:
FIELD1, FIELD2, FIELD3, FIELD4...
And I am hopeful that there's a way a can simply do something to drop that list of field names into a select statement like this:
SELECT (<my subquery, above>)
FROM MYTABLE;
Is this possible?
Assuming this situation
create table mytable ( a number, b number, c number)
insert into mytable values (10, 20, 30)
insert into mytable values (1, 2, 3)
and that only exists one table with that name (otherwise you should specify the owner in the query from all_tab_columns), your query could be simplified this way:
SELECT 'select ' || LISTAGG(column_name, ', ') WITHIN GROUP (ORDER BY column_name) || ' from ' || table_name
FROM all_tab_columns
WHERE TABLE_NAME = 'MYTABLE'
AND DATA_TYPE <> 'VARCHAR2'
AND DATA_LENGTH < 2000
GROUP BY table_name
this would give: select A, B, C from MYTABLE.
The problem here is that you can not simply run a statement that returns a variable number of columns; one way to use this could be building an xml:
SELECT xmltype(
DBMS_XMLGEN.getxml(
( SELECT 'select ' || LISTAGG(column_name, ', ') WITHIN GROUP (ORDER BY column_name) || ' from ' || table_name
FROM all_tab_columns
WHERE TABLE_NAME = 'MYTABLE'
AND DATA_TYPE <> 'VARCHAR2'
AND DATA_LENGTH < 2000
GROUP BY table_name)
)
)
FROM DUAL
<?xml version="1.0"?>
<ROWSET>
<ROW>
<A>10</A>
<B>20</B>
<C>30</C>
</ROW>
<ROW>
<A>1</A>
<B>2</B>
<C>3</C>
</ROW>
</ROWSET>
Another way could be using some PLSQL and dynamic SQL, with a little modification of yur query to concatenate the fields, to build the result in a unique string:
declare
type tTabResults is table of varchar2(1000);
vSQL varchar2(1000);
vTabResults tTabResults;
begin
SELECT 'select ' || LISTAGG( column_name, '|| '', '' ||') WITHIN GROUP (ORDER BY column_name) || ' from ' || table_name
into vSQL
FROM all_tab_columns
WHERE TABLE_NAME = 'MYTABLE'
AND DATA_TYPE <> 'VARCHAR2'
AND DATA_LENGTH < 2000
GROUP BY table_name;
--
execute immediate vSQL bulk collect into vTabResults;
--
for i in vTabResults.first .. vTabResults.last loop
dbms_output.put_line(vTabResults(i));
end loop;
end;
10, 20, 30
1, 2, 3
Notice that I oversimplified the problem, treating numbers as strings and not using any conversion, by simply printing the values in your table, no matter their type; in a real solution you should handle the possible types of your columns and modify the initial query to add some type conversions.
I am generating sql statements using this sql:
select 'select ' || listagg(t.column_name, ', ') within group (order by column_id, table_name) ||' from &#source_table# a' query from user_tab_cols t where t.table_name = '&#target_table#';
In some cases the columns I in the statement I generate are CLOB and I want to convert these to varchar2 using the following statement:
dbms_lob.substr (column, 4000, 1) as column and in other cases I want to remove the CLOB column, this I can control with a parameter.
How do I write the sql query?
I managed to create it using a case and a decode in the listagg:
select ' || listagg(
case t.data_type
when 'CLOB' then
decode('&#ignore_clob#',
'no', 'dbms_lob.substr ('||t.column_name||', 4000, 1) as '|| t.column_name,
'yes','')
else t.column_name
end
, ', ') within group (order by column_id, table_name) ||
' from #source_table a' query
from user_tab_cols t where t.table_name = '&#source_table#';