Search This Blog

Wednesday 4 January 2017

Display multiple rows in single row PL/SQL

Step 1:- Create table
CREATE TABLE TEST
(
      ID INTEGER,
      NAME VARCHAR2(50)
);

Step 2:- Insert records in table
INSERT INTO TEST VALUES('1','Ram');
INSERT INTO TEST VALUES('2','Shyam');
INSERT INTO TEST VALUES('3','Ghanshyam');

Step 3:- Select records from table
SELECT * FROM "TEST" t

ID
NAME
1
Ram
2
Shyam
3
Ghanshyam

Without Comma-separated
select rtrim(xmlagg(xmlelement(c, NAMEorder by NAME).extract('//text()')) as NAME
  from TEST;

NAME
GhanshyamRamShyam

Comma-separated
select rtrim(xmlagg(xmlelement(c, NAME || ','order by NAME)
             .extract('//text()'),
             ','as NAME
  from TEST;

NAME
Ghanshyam,Ram,Shyam

No comments:

Post a Comment