VARRAY error in PLSQL BLOCK [message #597632] |
Mon, 07 October 2013 04:27 |
|
mapps0999@gmail.com
Messages: 19 Registered: October 2013 Location: Bangalore
|
Junior Member |
|
|
Hi ALL,
I trying to write plsql anonymous block to return the name of all the employees who belongs to a specific department .
I am writing below script for this and getting the error where as the same logic if i use only in a function instead of creating it
within a procedure , i am not getting any error .. Please help me to fix this issue .
Script :
DECLARE
TYPE EMPARRAY is VARRAY(20) OF VARCHAR2(30);
FUNCTION getEmpArray (p_no IN NUMBER)
RETURN EMPARRAY
AS
l_data EmpArray := EmpArray();
CURSOR c_emp
IS
SELECT ename FROM scott.EMP
where deptno=p_no;
BEGIN
FOR emp_rec IN c_emp LOOP
l_data.extend;
l_data(l_data.count) := emp_rec.ename;
END LOOP;
--RETURN (l_data);
--dbms_output.put_line('v_tt');
END;
Error report:
ORA-06550: line 18, column 4:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
begin function package pragma procedure form
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
[EDITED by LF: fixed topic title typo; was "VARRY"]
[Updated on: Tue, 08 October 2013 09:52] by Moderator Report message to a moderator
|
|
|
|
|
|
|
Re: VARRY error in PLSQL BLOCK [message #597642 is a reply to message #597640] |
Mon, 07 October 2013 05:21 |
|
dariyoosh
Messages: 538 Registered: March 2009 Location: France
|
Senior Member |
|
|
I don't see any RETURN statement in your function.
In addition, looking at your code
. . .
SELECT getEmpArray (10)
INTO v_te
FROM dual;
. . .
getEmpArray, according to your code, is supposed to return a varray, yet you assign the result to VARCHAR2(30), that is assigning a composite type to a scalar type.
[Updated on: Mon, 07 October 2013 05:29] Report message to a moderator
|
|
|
|
|
|
|
|
Re: VARRY error in PLSQL BLOCK [message #597658 is a reply to message #597649] |
Mon, 07 October 2013 07:03 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
mapps0999@gmail.com wrote on Mon, 07 October 2013 16:48Hi , I changed the return to a varray variable still i am getting the error .
No, you are still doing it wrong. You cannot assign value to the object type itself. You need a declare a collection variable of that type.
SQL> DROP TABLE emp;
Table dropped
SQL> CREATE TABLE emp(ename VARCHAR2(10), deptno NUMBER);
Table created
SQL> INSERT INTO emp VALUEs('lalit',10);
1 row inserted
SQL> COMMIT;
Commit complete
SQL> SET SERVEROUTPUT ON ;
SQL> DECLARE
2 TYPE EMPARRAY IS TABLE OF EMP.ENAME%TYPE;
3 L_DATA EMPARRAY:=EMPARRAY();
4 V_TE EMPARRAY:= EMPARRAY();
5 FUNCTION GETEMPARRAY(P_NO IN NUMBER) RETURN EMPARRAY AS
6
7 BEGIN
8
9 FOR EMP_REC IN (SELECT ENAME FROM EMP WHERE DEPTNO = P_NO) LOOP
10 L_DATA.EXTEND;
11 L_DATA(L_DATA.COUNT) := EMP_REC.ENAME;
12 END LOOP;
13 RETURN(L_DATA);
14 END;
15 BEGIN
16 V_TE := GETEMPARRAY(10);
17 DBMS_OUTPUT.PUT_LINE(V_TE(1));
18 END;
19 /
lalit
PL/SQL procedure successfully completed
Since EMP table is already present which would suffice for declaring a type so I did not take a VARRAY explicitly.
Regards,
Lalit
|
|
|
|
|
Re: VARRY error in PLSQL BLOCK [message #597671 is a reply to message #597670] |
Mon, 07 October 2013 08:18 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
mapps0999@gmail.com wrote on Mon, 07 October 2013 18:40Thank you lalit for your solution . Here is there any solution to display all the ename of the respective department dynamically .
n number of employee may be there in a specific department. I want all those values into the variable dynamically .
Just think once about it, you have already used it in your code....A FOR LOOP, isn't it?
SQL> DROP TABLE emp;
Table dropped
SQL> CREATE TABLE emp(ename VARCHAR2(10), deptno NUMBER);
Table created
SQL> INSERT INTO emp VALUEs('lalit',10);
1 row inserted
SQL> INSERT INTO emp VALUEs('OraFAQ',10);
1 row inserted
SQL> INSERT INTO emp VALUEs('mapps0999',10);
1 row inserted
SQL> COMMIT;
Commit complete
SQL> SET SERVEROUTPUT ON ;
SQL> DECLARE
2 TYPE EMPARRAY IS TABLE OF EMP.ENAME%TYPE;
3 L_DATA EMPARRAY := EMPARRAY();
4 V_TE EMPARRAY := EMPARRAY();
5 FUNCTION GETEMPARRAY(P_NO IN NUMBER) RETURN EMPARRAY AS
6
7 BEGIN
8
9 FOR EMP_REC IN (SELECT ENAME FROM EMP WHERE DEPTNO = P_NO) LOOP
10 L_DATA.EXTEND;
11 L_DATA(L_DATA.COUNT) := EMP_REC.ENAME;
12 END LOOP;
13 RETURN(L_DATA);
14 END;
15 BEGIN
16 V_TE := GETEMPARRAY(10);
17 FOR I IN 1 .. V_TE.COUNT LOOP
18 DBMS_OUTPUT.PUT_LINE(V_TE(I));
19 END LOOP;
20 END;
21 /
lalit
OraFAQ
mapps0999
PL/SQL procedure successfully completed
Regards,
Lalit
|
|
|
|
|
|
|
Re: VARRY error in PLSQL BLOCK [message #597714 is a reply to message #597691] |
Mon, 07 October 2013 14:58 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
mapps0999@gmail.com wrote on Mon, 07 October 2013 21:47Hi Lalit ,
the below mentioned script returns me on loop the series of data but i want them in in a single row for each parameter entered, in a row terminated by '-' instead of one column
Desired output is
lalit-OraFAQ-mapps0999
Please suggest ....
Now I should not give you any direct answers. You have not followed any of the forum guidelines which everybody has suggested. And this last question about "PIVOT", it is very easy to search on google and find.
I insist, you search for LISTAGG function, and come back with your test case. And at least for one last time, use code tags with proper formatting of code.
Regards,
Lalit
|
|
|
|
|
|
Re: VARRY error in PLSQL BLOCK [message #597730 is a reply to message #597726] |
Mon, 07 October 2013 22:56 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
mapps0999@gmail.com wrote on Tue, 08 October 2013 09:13
It throws me an error ... please suggest if anything is wrong here.....
What error? Copy paste the SQL*Plus session.
Another thing, when all this could be done in a single SQL statement, then what was that anonymous block with a function etc. all about? What exactly is your requirement?
|
|
|
Re: VARRY error in PLSQL BLOCK [message #597731 is a reply to message #597728] |
Mon, 07 October 2013 22:59 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
mapps0999@gmail.com wrote on Tue, 08 October 2013 09:19Hi.... I am using 10.2.0.4.0 version ... i think this version doesn't support listagg function ... please suggest anyother approach .
You mentioned 11g in the title, so I suggested LISTAGG. Anyway, you can search a bit to find alternative to LISTAGG. It is a FAQ, search this forum.
|
|
|
|
|
Re: VARRY error in PLSQL BLOCK [message #597868 is a reply to message #597728] |
Wed, 09 October 2013 00:23 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
mapps0999@gmail.com wrote on Tue, 08 October 2013 09:19Hi.... I am using 10.2.0.4.0 version ... i think this version doesn't support listagg function ... please suggest anyother approach .
If you can do it in plain SQL then why do you need PL/SQL?
Since you say you are not in 11g, there is one way using ROW_NUMBER() and SYS_CONNECT_BY_PATH which works in 9i.
SQL> DROP TABLE EMP;
Table dropped
SQL> CREATE TABLE emp(ename VARCHAR2(10), deptno NUMBER);
Table created
SQL> INSERT INTO emp VALUEs('lalit',10);
1 row inserted
SQL> INSERT INTO emp VALUEs('OraFAQ',10);
1 row inserted
SQL> INSERT INTO emp VALUEs('mapps0999',10);
1 row inserted
SQL> COMMIT;
Commit complete
SQL> SELECT DEPTNO,
2 LTRIM(MAX(SYS_CONNECT_BY_PATH(ENAME, ' - '))
3 KEEP(DENSE_RANK LAST ORDER BY CURR),
4 ' - ') AS EMPLOYEES
5 FROM (SELECT DEPTNO,
6 ENAME,
7 ROW_NUMBER() OVER(PARTITION BY DEPTNO ORDER BY ENAME) AS CURR,
8 ROW_NUMBER() OVER(PARTITION BY DEPTNO ORDER BY ENAME) - 1 AS PREV
9 FROM EMP)
10 GROUP BY DEPTNO
11 CONNECT BY PREV = PRIOR CURR
12 AND DEPTNO = PRIOR DEPTNO
13 START WITH CURR = 1;
DEPTNO EMPLOYEES
---------- --------------------------------------------------------------------------------
10 OraFAQ - lalit - mapps0999
Regards,
Lalit
|
|
|