Q
Problem solve Get help with specific problems with your technologies, process and projects.

Query to view all emp_id in a single column

I have a table with employee name as emp_id column. I want a result of all emp_id in a single column as comma separated. In SQL Server (T-SQL), the following query gives the correct result.
DECLARE @EmployeeList varchar(5000)
SELECT @EmployeeList = COALESCE(@EmployeeList + ', ', '') + 
   CAST(ltrim(rtrim(emp_id )) AS varchar(20))
FROM employee
SELECT @EmployeeList

I need results in Oracle with PL/SQL as well.
Here you go:

DECLARE
 result_set varchar2(4000);
 cursor c1 is select emd_id from employee;
BEGIN
   FOR c1rec in c1 LOOP
    result_set := result_set || ',' || c1rec.emp_id;
  END LOOP;
   dbms_output.put_line(ltrim(result_set, ','));
END;

Dig Deeper on Using Oracle PL-SQL

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close