Ask the Expert

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.

    Requires Free Membership to View

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

This was first published in March 2004

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: