I have a table named Data as below
g_name g_id v_data
----- ---- ------
Test 123 ABC
Test 123 DEG
Test 123 None
Test 123
Test 123 HIJ
I want one select query which returns me values like below(in Oracle Database):
Value
------------
Test,123,ABC
Test,123,DEG
...
...
...
LISTAGG()
function is not working in Oracle Forms Builder 12c
.
There may be more than 3 columns. Thus, I need a dynamic way in order to combine all columns together.
Can someone help me out here?
You can concat the values:
select g_name || ',' || g_id || ',' || v_data
from t
Please perform concatenation
select g_name||','||g_id||','||v_data from data;
You can write a dynamic query in plsql and achieve this.
select LISTAGG(column_name, ',') WITHIN GROUP (ORDER BY column_name) as colum_name from cols where table_name = 'DATA';
Pass this result to a column using into
clause.
If you have an option to create a function in your database, perhaps you can retrieve the result of the function as the query you want to execute
Let me show you an example:
1.I create a table with 6 columns.
create table test.mytest ( c1 number, c2 number , c3 number, c4 varchar2(10) , c5 number, c6 number );
2.I create a function to retrieve the query ( or you can invoke as an anonymus plsql block )
create or replace function test.p_gen_query ( powner in varchar2 , ptable in varchar2, p_pretty in varchar2 default 'N' )
return clob
as
vowner varchar2(40) := upper(powner);
vtable varchar2(128) := upper(ptable);
vconta pls_integer;
vsql clob;
out_string varchar2(128);
cursor c_tab_columns
is
select column_name, count(*) over () tot_rows
from all_tab_columns where table_name = vtable and owner = vowner
order by column_id;
begin
vsql := ' select ' ;
for item in c_tab_columns
loop
out_string := item.COLUMN_NAME;
if c_tab_columns%rowcount = 1 then
dbms_lob.append(vsql,out_string||' || '','' || ');
elsif c_tab_columns%rowcount < item.tot_rows then
dbms_lob.append(vsql,out_string||' || '','' || ');
else
dbms_lob.append(vsql,out_string||' from '||vowner||'.'||vtable||' ');
end if;
end loop;
if p_pretty = 'Y'
then
dbms_lob.append(vsql,''||chr(10)||'');
dbms_lob.append(vsql,' ; ');
end if;
return vsql;
end;
/
3.Call the function with p_pretty as 'N' ( retrieves the query without sql terminator )
SQL> set long 99999999 longchunksize 9999 lines 9999 pages 0
SQL> select test.p_gen_query ( 'TEST' , 'MYTEST' ) from dual ;
select C1 || ',' || C2 || ',' || C3 || ',' || C4 || ',' || C5 || ',' || C6 from TEST.MYTEST
4.Call the function with p_pretty as 'Y' to get it with sql terminator
SQL> select test.p_gen_query ( 'TEST' , 'MYTEST' , 'Y' ) from dual ;
select C1 || ',' || C2 || ',' || C3 || ',' || C4 || ',' || C5 || ',' || C6 from TEST.MYTEST
;
This way you can use it for any query you want to build in that way. I guess you can retrieve the result from Oracle Forms.
First of all, you need a primary key column ( call id
which I asssume as the first column within the columns list ) for the table.
Then you need to create a stored function which contains both LISTAGG()
function, which will be used to combine column names derived from one of the dictionary views giving column names such as user_tab_columns
, user_tab_cols
or just col
through concatenetion, and Dynamic Query returning the concatenated columns for each id
value.
So, create the function within the database by;
CREATE OR REPLACE FUNCTION get_value( i_id Data.id%type ) RETURN VARCHAR2 IS
v_out VARCHAR2(32767);
BEGIN
SELECT LISTAGG(column_name, '|| '','' ||') WITHIN GROUP (ORDER BY column_id)
INTO v_out
FROM cols
WHERE table_name = 'DATA'
AND column_id > 1;
EXECUTE IMMEDIATE 'SELECT '||v_out||' FROM Data WHERE id = :v_id' INTO v_out USING i_id;
RETURN v_out;
END;
/
And call from Forms as a regular query statement:
SELECT id, get_value( id ) AS value
FROM Data
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.