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.
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
Hi,
Try this -
CREATE OR REPLACE
-
FUNCTION fnGetRolesByEmployeeID
-
(
-
v_EmployeeID IN char
-
)
-
RETURN VARCHAR2
-
AS
-
v_roles_string VARCHAR2(4000);
-
BEGIN
-
-
WITH v_txns AS (
-
SELECT R.ROLE 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 )
-
)
-
SELECT MAX (SYS_CONNECT_BY_PATH (v_Roles, ', ')) INTO v_roles_string
-
FROM
-
(SELECT v_roles_string, ROW_NUMBER () OVER (ORDER BY trans_id) AS curr
-
FROM v_txns)
-
CONNECT BY curr - 1 = PRIOR curr
-
START WITH curr = 1;
-
-
v_roles_string := SUBSTR (v_roles_string, 2);
-
-
RETURN v_roles_string;
-
END;
-
THanks
Rajkumar
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...
Hi,
See the below Query, v_txns is an Object that holds the records, No need to declare the v_txns -
CREATE OR REPLACE
-
FUNCTION fnGetRolesByEmployeeID
-
(
-
v_EmployeeID IN VARCHAR2
-
)
-
RETURN VARCHAR2
-
AS
-
v_roles_string VARCHAR2(4000);
-
v_Roles VARCHAR2(4000);
-
BEGIN
-
-
WITH v_txns AS (
-
SELECT R.ROLE 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 )
-
)
-
SELECT MAX (SYS_CONNECT_BY_PATH (v_Roles, ', ')) INTO v_roles_string
-
FROM
-
(SELECT v_Roles, ROW_NUMBER () OVER (ORDER BY v_Roles) AS curr
-
FROM v_txns)
-
CONNECT BY curr - 1 = PRIOR curr
-
START WITH curr = 1;
-
-
v_roles_string := SUBSTR (v_roles_string, 2);
-
-
RETURN v_roles_string;
-
END;
-
-
select fnGetRolesByEmployeeID('614728') from dual;
-
OR
This is an another way to form a string separated with comma -
SELECT distinct RTRIM(XMLAGG(XMLELEMENT(e, R.ROLE || ',')).EXTRACT('//text()'),',') Role
-
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 )
-
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
Hi, rajujrk,
Your Code was awesome , thanks for your solution, Thanks , thanks a lot. :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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?
|
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...
|
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,...
|
by: benben |
last post by:
I am looking for a good example of overloading operator , (operator comma)
Any suggestions?
Ben
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
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
|
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...
| |