473,320 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I Create a Comma-Separated List using a PL/SQL Query?

20
Hi experts,
I have T-SQL funtion, here the query is used to list the data separated by comma.how can we write in PL/SQL.

role
table for example: -------
audit
cement
mason
result should be: audit,cement,mason.

ALTER FUNCTION dbo.fnGetRolesByEmployeeID
(
@EmployeeID uniqueidentifier
)
RETURNS varchar(max)
AS
BEGIN

DECLARE @Roles varchar(max)

SELECT @Roles = COALESCE(@Roles + ' , ', '') + R.Role
FROM Employees AS E LEFT OUTER JOIN
UsersInRoles AS U ON E.EmployeeID = U.UserID LEFT OUTER JOIN
Roles AS R ON U.RoleID = R.RoleID
WHERE (E.EmployeeID = @EmployeeID)

RETURN @Roles
END


I Converted in pl/SQL like this,
create or replace
FUNCTION fnGetRolesByEmployeeID
(
v_EmployeeID IN char
)
RETURN VARCHAR2
AS
v_Roles VARCHAR2(4000);

BEGIN
SELECT coalesce(v_Roles || ' , ', '') || R.ROLE

INTO v_Roles
FROM Employees E
LEFT JOIN UsersInRoles U
ON E.EmployeeID = U.UserID
LEFT JOIN Roles R
ON U.RoleID = R.RoleID
WHERE ( E.EmployeeID = v_EmployeeID ) ;
RETURN v_roles;
END;



but this query not fetches two values , could any one provide the solution, it would be appreciated.
Jul 5 '12 #1

✓ answered by rajujrk

Hi

Are you trying this Query in ORACLE? OR other Databases like MS SQL SERVER or MYSQL?

Cause this Forum is for Oracle Database, and My Query will Work in Oracle DB

5 8574
rajujrk
107 100+
Hi,

Try this

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE 
  2. FUNCTION fnGetRolesByEmployeeID
  3. (
  4. v_EmployeeID IN char
  5. )
  6. RETURN VARCHAR2
  7. AS
  8. v_roles_string VARCHAR2(4000);
  9. BEGIN
  10.  
  11. WITH v_txns AS (
  12.  SELECT R.ROLE v_Roles
  13. FROM Employees E
  14. LEFT JOIN UsersInRoles U
  15. ON E.EmployeeID = U.UserID
  16. LEFT JOIN Roles R
  17. ON U.RoleID = R.RoleID
  18. WHERE ( E.EmployeeID = v_EmployeeID ) 
  19.  )
  20.  SELECT MAX (SYS_CONNECT_BY_PATH (v_Roles, ', ')) INTO v_roles_string
  21.  FROM
  22.   (SELECT v_roles_string, ROW_NUMBER () OVER (ORDER BY trans_id) AS curr
  23.     FROM v_txns)
  24.  CONNECT BY curr - 1 = PRIOR curr
  25.  START WITH curr = 1;
  26.  
  27.  v_roles_string := SUBSTR (v_roles_string, 2);
  28.  
  29. RETURN v_roles_string;
  30. END;
  31.  

THanks
Rajkumar
Aug 20 '12 #2
bharthi
20
Hi ... i have one doubt in the above query can you please explain it.. WHAT IS THAT "v_txns".. i getting the error due to this "v_txns"... help me.. please . thanks again...
Aug 24 '12 #3
rajujrk
107 100+
Hi,

See the below Query, v_txns is an Object that holds the records, No need to declare the v_txns

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE 
  2. FUNCTION fnGetRolesByEmployeeID
  3. (
  4. v_EmployeeID IN VARCHAR2
  5. )
  6. RETURN VARCHAR2
  7. AS
  8. v_roles_string VARCHAR2(4000);
  9. v_Roles  VARCHAR2(4000);
  10. BEGIN
  11.  
  12. WITH v_txns AS (
  13.          SELECT R.ROLE v_Roles
  14.             FROM Employees E
  15.             LEFT JOIN UsersInRoles U
  16.             ON E.EmployeeID = U.UserID
  17.             LEFT JOIN Roles R
  18.             ON U.RoleID = R.RoleID
  19.             WHERE ( E.EmployeeID = v_EmployeeID ) 
  20.  )
  21.  SELECT MAX (SYS_CONNECT_BY_PATH (v_Roles, ', ')) INTO v_roles_string
  22.  FROM
  23.   (SELECT v_Roles, ROW_NUMBER () OVER (ORDER BY v_Roles) AS curr
  24.     FROM v_txns)
  25.  CONNECT BY curr - 1 = PRIOR curr
  26.  START WITH curr = 1;
  27.  
  28.  v_roles_string := SUBSTR (v_roles_string, 2);
  29.  
  30. RETURN v_roles_string;
  31. END;
  32.  
  33. select fnGetRolesByEmployeeID('614728') from dual;
  34.  
OR

This is an another way to form a string separated with comma

Expand|Select|Wrap|Line Numbers
  1. SELECT distinct RTRIM(XMLAGG(XMLELEMENT(e, R.ROLE || ',')).EXTRACT('//text()'),',') Role
  2.             FROM Employees E
  3.             LEFT JOIN UsersInRoles U
  4.             ON E.EmployeeID = U.UserID
  5.             LEFT JOIN Roles R
  6.             ON U.RoleID = R.RoleID
  7.             WHERE ( E.EmployeeID = v_EmployeeID ) 
  8.  
Aug 24 '12 #4
rajujrk
107 100+
Hi

Are you trying this Query in ORACLE? OR other Databases like MS SQL SERVER or MYSQL?

Cause this Forum is for Oracle Database, and My Query will Work in Oracle DB
Aug 24 '12 #5
bharthi
20
Hi, rajujrk,
Your Code was awesome , thanks for your solution, Thanks , thanks a lot. :)
Aug 26 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

27
by: Alberto Vera | last post by:
Hello: I have the next structure: How Can I make it using Python? How Can I update the value of 6?
4
by: Arne | last post by:
From: "Arne de Booij" <a_de_booij@hotmail.com> Subject: Comma delimited array into DB problems Date: 9. februar 2004 10:39 Hi, I have an asp page that takes input from a form on the previous...
5
by: Derek | last post by:
I came upon the idea of writting a logging class that uses a Python-ish syntax that's easy on the eyes (IMO): int x = 1; double y = 2.5; std::string z = "result"; debug = "Results:", x, y,...
2
by: benben | last post by:
I am looking for a good example of overloading operator , (operator comma) Any suggestions? Ben
2
by: Jack | last post by:
Hi, I have form where one has to input officer's salary along with other data. The officer's salary field is tied to a currency field in backend Access. Right now folks enter numbers e.g....
3
by: SteelDetailer | last post by:
Thnaks in advance for considering this post. It's probably very simple, but..... I have an old VB6 application that allows me to create, save and edit a "project information file" that is a...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
1
by: Yama | last post by:
Hi, I am looking to create a report comma delimited on a click of a button. Explanantion: 1. Get from the database: "SELECT * FROM Customers WHERE Region = 'CA'" 2. Use either DataReader or...
15
by: Lighter | last post by:
In 5.3.3.4 of the standard, the standard provides that "The lvalue-to- rvalue(4.1), array-to-pointer(4.2),and function-to-pointer(4.3) standard conversions are not applied to the operand of...
0
by: Kristi | last post by:
I need to create a CL program that will take a PF, and create a tab delimited file that has comma seperated column headings as the first record. I know i can use cpytostmf/cpytoimpf to create the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.