Home » SQL & PL/SQL » SQL & PL/SQL » XMLAGG Function (Oracle 11g)
XMLAGG Function [message #616994] Tue, 24 June 2014 01:30 Go to next message
vicktorz
Messages: 26
Registered: March 2013
Junior Member
Hi All,

Your help is appreciated here..

I have a table with multiple rows of data.. i want to print all rows values in single column enclosed with single quotes..

Example:

table Domain

Domain_name
***********
vs
LS
RS
AS

Output should be like= 'vs','LS','RS','AS'

hOW do this operation using XMLAGG function..

I'm new to pl/SQL, Kidnly guide me..
Re: XMLAGG Function [message #616996 is a reply to message #616994] Tue, 24 June 2014 01:52 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
If XMLAGG is not mandatory to use, then LISTAGG would do the work too :

SQL> WITH DATA AS(
  2  SELECT 'vs' str FROM dual UNION ALL
  3  SELECT 'LS' FROM dual UNION ALL
  4  SELECT 'RS' FROM dual UNION ALL
  5  SELECT 'AS' FROM dual)
  6  SELECT ''''||listagg(str,''',''') WITHIN GROUP(ORDER BY rn)||'''' str FROM
  7  (SELECT ROWNUM rn, str FROM DATA);
STR
--------------------------------------------------------------------------------
'vs','LS','RS','AS'



Regards,
Lalit
icon7.gif  Re: XMLAGG Function [message #616997 is a reply to message #616996] Tue, 24 June 2014 02:02 Go to previous messageGo to next message
vicktorz
Messages: 26
Registered: March 2013
Junior Member
Thank you Lalit..

But here, values VS,LS,RS,AS are sample data only.. it may change in future upto infinite values in the table..

so i need to write query dynamically..

In your query, manually selecting values and unioning everytime will not be feasiable..

Please consider table info like
Domain_name
***********
vs
LS
RS
AS
..
.
.
.
ZS



Re: XMLAGG Function [message #616999 is a reply to message #616997] Tue, 24 June 2014 02:06 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Lalit's query does not depend on the number of rows and values in your table.

Quote:
In your query, manually selecting values and unioning everytime will not be feasiable..


He had to do this because you didn't provide a test case.
You have to realize we have not your table and data, so we have to simulate it, this is the purpose of WITH clause here.

[Updated on: Tue, 24 June 2014 02:08]

Report message to a moderator

Re: XMLAGG Function [message #617000 is a reply to message #616997] Tue, 24 June 2014 02:08 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
vicktorz wrote on Tue, 24 June 2014 12:32
But here, values VS,LS,RS,AS are sample data only.. it may change in future upto infinite values in the table..

so i need to write query dynamically..

In your query, manually selecting values and unioning everytime will not be feasiable


LOL...The WITH clause is just like your table. Since you did not provide any test case, I had to do it manually.

All you need to do is, remove the WITH clause and just keep the same query and replace DATA with your table_name.

SELECT ''''||listagg(str,''',''') WITHIN GROUP(ORDER BY rn)||'''' str 
FROM
    (SELECT ROWNUM rn, str FROM <your_table_name>);


Makes sense?


Regards,
Lalit
Re: XMLAGG Function [message #617002 is a reply to message #617000] Tue, 24 June 2014 02:20 Go to previous messageGo to next message
vicktorz
Messages: 26
Registered: March 2013
Junior Member
Yes.. I understood and it is working as i expected..


Thank you Lalit & Michel:)


-Vijay
Re: XMLAGG Function [message #617008 is a reply to message #617002] Tue, 24 June 2014 03:19 Go to previous messageGo to next message
vicktorz
Messages: 26
Registered: March 2013
Junior Member
Hi Lalit& Michel..

For below code i'm not getting correct output..

Code:
-----

declare
a varchar2(200) :='vs';
b varchar2(500);
begin
SELECT ''''||listagg(name,''',''') WITHIN GROUP(ORDER BY rn)||'''' str into b FROM
(SELECT ROWNUM rn, name FROM vk_domain);

dbms_output.put_line('input='||a);
dbms_output.put_line('output='||b);
if a in (b)
then
dbms_output.put_line('yes');
else
dbms_output.put_line('no');
end if;
--end loop;
end;
/
show error



Table Information: Table Name- vk_domain
------------------

name
*****
vs
LS
RS
AS

i'm getting below output:

input=vs
output='vs','LS','RS','AS'
no


Value 'vs' is in the list of table vk_domain.But i'm getting output like it is not in the list.. Could you pls tell me where i commited wrong??
Re: XMLAGG Function [message #617009 is a reply to message #617008] Tue, 24 June 2014 03:22 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
This is a variation on the varying in list problem.
As far as oracle is concerned b is a single value, not a set of values.
Re: XMLAGG Function [message #617015 is a reply to message #617008] Tue, 24 June 2014 03:50 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So what you want to do is:
SQL> declare
  2    a varchar2(200) :='vs';
  3    b varchar2(200);
  4  begin
  5    SELECT name into b FROM vk_domain WHERE name = a and rownum = 1;
  6    dbms_output.put_line('yes');
  7  exception
  8    when no_data_found then dbms_output.put_line('no');
  9  end;
 10  /
yes

PL/SQL procedure successfully completed.

Re: XMLAGG Function [message #617033 is a reply to message #617015] Tue, 24 June 2014 04:59 Go to previous messageGo to next message
vicktorz
Messages: 26
Registered: March 2013
Junior Member
Great;)

Thank you Michel:)
Re: XMLAGG Function [message #617034 is a reply to message #617033] Tue, 24 June 2014 05:05 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
vicktorz wrote on Tue, 24 June 2014 15:29
Great;)

Thank you Michel:)


But Michel did not use varying in list at all(he helped you to understand) and your code was to look for a value in a set of values(rather a list of values concatenated as a string). So what were you tring to achieve?
Previous Topic: Arabic letters not displayed in xmltype table
Next Topic: For all update clause
Goto Forum:
  


Current Time: Thu Apr 25 17:04:00 CDT 2024