Home » SQL & PL/SQL » SQL & PL/SQL » ora-06550 (plsql)
ora-06550 [message #629465] Tue, 09 December 2014 09:32 Go to next message
sanodani
Messages: 98
Registered: October 2014
Member
Hallo all..
I am trying to solve this Problem with Bulk collect but have Problem with the error:
ORA-06550
PLS - 00653

can anyone please help to find out my error or some help? Smile
thankx

 
Create or Replace Package emp20 is
   
TYPE t_emptyptab is table of employees%ROWTYPE;

Type ref_cur is ref cursor;
    return employees%ROWTYPE;

Function getEmpNTab2 (pValue in Varchar2)            
    Return t_emptyptab PIPELINED;

End emp20;
/

Create or Replace Package Body emp20 is

Function getEmpNTab2 (pValue in Varchar2)           
    Return t_emptyptab PIPELINED 
    is
    
    vSQL    Varchar2(2000);
    vTab    t_emptyptab;
Begin
    if pValue = 'alt' then
        vSQL    := 'SELECT e.empno employee_id, Null first_name, e.ename last_name '
                   ||      ', Null email, Null phone_number, e.hiredate hire_date '
                   ||      ', e.job job_id, e.sal salary '
                   ||      ', ROUND(e.comm/e.sal,2),99/100) commission_pct '
                   ||      ', e.mgr manager_id, e.deptno department_id '
                   ||      ', FROM emp e ';
    elsif pValue = 'neu' then
        vSQL    := 'SELECT e.* FROM employees e' ;
    end if;
    
    EXECUTE IMMEDIATE vSQL BULK COLLECT into vTab;
    FOR i in vTab.first..vTab.last loop
            pipe row (vTab(i));
        end loop;
        return;
end getEmpNTab2;
  
end emp20;
/

Declare 
    vTab emp20.t_emptyptab  := emp20.t_emptyptab();
    idx integer;
    
Begin
    vTab    := emp20.getEmpNTab2('alt');
    
    idx := vTab.first;
    
    loop
        exit when idx = vTab.last;
            dbms_output.put_line(vTab(idx).employee_id || ' ' || vTab(idx).last_name||' '|| vTab(idx).first_name);
       idx  := vTab.next(idx);
    end loop;
end;

/


[mod-edit] JD changed quote to code tags.

[Updated on: Tue, 09 December 2014 09:36] by Moderator

Report message to a moderator

Re: ora-06550 [message #629466 is a reply to message #629465] Tue, 09 December 2014 09:41 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
1. Open SQL*Plus.
2. Remove any EXCEPTION BLOCK from your PL/SQL code(as of now, for the sake of debugging).
3. Compile your PL/SQL code.
4. Type SHOW ERRORS and press enter.
5. Copy & paste the error stack along with the PL/SQL code.

A moderator has applied code tags, however, you are expected to do it yourself. Please read the sticky post on top of this forum. It has been kept above for some reason. Ignoring the posting guideliness is not a good thing on any forum. Apart from this forum, there are numerous articles over the web about "How to ask a good question on a forum".

If not too much to ask, what sort of debugging do you usually do as a developer? Just curious to know.

[Updated on: Tue, 09 December 2014 09:46]

Report message to a moderator

Re: ora-06550 [message #629471 is a reply to message #629465] Tue, 09 December 2014 10:24 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> Create or Replace Package emp20 is
  2
  3  TYPE t_emptyptab is table of employees%ROWTYPE;
  4
  5  Type ref_cur is ref cursor;
  6      return employees%ROWTYPE;
  7
  8  Function getEmpNTab2 (pValue in Varchar2)
  9      Return t_emptyptab PIPELINED;
 10
 11  End emp20;
 12  /

Package created.

SQL> Create or Replace Package Body emp20 is
  2
  3  Function getEmpNTab2 (pValue in Varchar2)
  4      Return t_emptyptab PIPELINED
  5      is
  6
  7      vSQL    Varchar2(2000);
  8      vTab    t_emptyptab;
  9  Begin
 10      if pValue = 'alt' then
 11          vSQL    := 'SELECT e.empno employee_id, Null first_name, e.ename last_name '
 12                     ||      ', Null email, Null phone_number, e.hiredate hire_date '
 13                     ||      ', e.job job_id, e.sal salary '
 14                     ||      ', ROUND(e.comm/e.sal,2),99/100) commission_pct '
 15                     ||      ', e.mgr manager_id, e.deptno department_id '
 16                     ||      ', FROM emp e ';
 17      elsif pValue = 'neu' then
 18          vSQL    := 'SELECT e.* FROM employees e' ;
 19      end if;
 20
 21      EXECUTE IMMEDIATE vSQL BULK COLLECT into vTab;
 22      FOR i in vTab.first..vTab.last loop
 23              pipe row (vTab(i));
 24          end loop;
 25          return;
 26  end getEmpNTab2;
 27
 28  end emp20;
 29  /

Package body created.

SQL> Declare
  2      vTab emp20.t_emptyptab  := emp20.t_emptyptab();
  3      idx integer;
  4
  5  Begin
  6      vTab    := emp20.getEmpNTab2('alt');
  7
  8      idx := vTab.first;
  9
 10      loop
 11          exit when idx = vTab.last;
 12              dbms_output.put_line(vTab(idx).employee_id || ' ' || vTab(idx).last_name||' '|| vTab(idx).first_name);
 13         idx  := vTab.next(idx);
 14      end loop;
 15  end;
 16
 17  /
    idx := vTab.first;
         *
ERROR at line 8:
ORA-06550: line 8, column 10:
PLS-00653: aggregate/table functions are not allowed in PL/SQL scope


getEmpNTab2 is a pipelined function you do not access/query a pipelined in this way.

PL/SQL User's Guide and Reference
Application Developer's Guide - Fundamentals

Re: ora-06550 [message #629472 is a reply to message #629471] Tue, 09 December 2014 10:33 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
If you don't use a pipelined function then you can do it in your (not recommended) way:
SQL> Create or Replace Package emp20 is
  2
  3  TYPE t_emptyptab is table of employees%ROWTYPE;
  4
  5  Type ref_cur is ref cursor;
  6      return employees%ROWTYPE;
  7
  8  Function getEmpNTab2 (pValue in Varchar2)
  9      Return t_emptyptab ;
 10
 11  End emp20;
 12  /

Package created.

SQL> sho err
No errors.
SQL> Create or Replace Package Body emp20 is
  2
  3  Function getEmpNTab2 (pValue in Varchar2)
  4      Return t_emptyptab
  5  is
  6      vSQL    Varchar2(2000);
  7      vTab    t_emptyptab;
  8  Begin
  9      if pValue = 'alt' then
 10          vSQL    := 'SELECT e.empno employee_id, Null first_name, e.ename last_name '
 11                     ||      ', Null email, Null phone_number, e.hiredate hire_date '
 12                     ||      ', e.job job_id, e.sal salary '
 13                     ||      ', ROUND(e.comm/e.sal,2),99/100) commission_pct '
 14                     ||      ', e.mgr manager_id, e.deptno department_id '
 15                     ||      ', FROM emp e ';
 16      elsif pValue = 'neu' then
 17          vSQL    := 'SELECT e.* FROM employees e' ;
 18      end if;
 19
 20      EXECUTE IMMEDIATE vSQL BULK COLLECT into vTab;
 21      return vTab;
 22  end getEmpNTab2 ;
 23
 24  end emp20;
 25  /

Package body created.

SQL> sho err
No errors.
SQL> Declare
  2      vTab emp20.t_emptyptab  := emp20.t_emptyptab();
  3      idx integer;
  4
  5  Begin
  6      vTab    := emp20.getEmpNTab2('alt');
  7
  8      idx := vTab.first;
  9
 10      loop
 11          exit when idx = vTab.last;
 12              dbms_output.put_line(vTab(idx).employee_id || ' ' || vTab(idx).last_name||' '|| vTab(idx).first_name);
 13         idx  := vTab.next(idx);
 14      end loop;
 15  end;
 16  /
Declare
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected
ORA-06512: at "MICHEL.EMP20", line 20
ORA-06512: at line 6

Now you have to fix your query.

[Updated on: Wed, 10 December 2014 00:46]

Report message to a moderator

Re: ora-06550 [message #629479 is a reply to message #629465] Tue, 09 December 2014 11:44 Go to previous messageGo to next message
Shreya Aggarwal
Messages: 4
Registered: December 2014
Junior Member
Hi,

Data returned by pipelined function is accessed in a slight different way.

Syntax to use the same is:

SELECT * FROM TABLE(pipelined_function(parameter_1,...));

So here you can make your block working by replacing call to the pipelined function as follows:

Declare
vTab emp20.t_emptyptab := emp20.t_emptyptab();
idx integer;
Begin
--vTab := emp20.getEmpNTab2('alt');
/* modified query*/
SELECT *
BULK COLLECT
INTO vTab
FROM TABLE(TEST.getEmpNTab2('alt'));

idx := vTab.first;
loop
exit when idx = vTab.last;
dbms_output.put_line(vTab(idx).employee_id || ' ' || vTab(idx).last_name||' '|| vTab(idx).first_name);
idx := vTab.next(idx);
end loop;
end;

Thanks.
Re: ora-06550 [message #629480 is a reply to message #629479] Tue, 09 December 2014 11:53 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

@Shreya Aggarwal,

Welcome to the forum.
Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

Note that we prefer to let people learn by themselves instead of providing a complete solution above all when they are students.

Tell me and I'll forget; show me and I may remember; involve me and I'll understand

[Updated on: Tue, 09 December 2014 11:54]

Report message to a moderator

Re: ora-06550 [message #629490 is a reply to message #629480] Tue, 09 December 2014 15:23 Go to previous messageGo to next message
sanodani
Messages: 98
Registered: October 2014
Member
Hallo all
thank you very much for your reply
well, i did change my code without pipeline function, :/ but still i am getting error:
ORA-00907: missing right parenthesis
ORA-06512: at "HR.EMP20", line 21
ORA-06512: at line 4

but i did not see where the parenthesis is missing .. Sad please help me

my new code to execute function is :

Declare 
    vTab    emp20.t_emptyptab;
Begin
    vTab    := emp20.getEmpNTab2('alt');
    For idx IN 1 .. vTab.COUNT
    loop
       dbms_output.put_line(vTab(idx).employee_id || ' ' || vTab(idx).last_name||' '|| vTab(idx).salary);
    end loop;
end;
Re: ora-06550 [message #629492 is a reply to message #629490] Tue, 09 December 2014 16:28 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>but i did not see where the parenthesis is missing

We can not see where it is missing, either since you decided to not post the code which generates the error.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/
Re: ora-06550 [message #629502 is a reply to message #629490] Wed, 10 December 2014 00:46 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I told you: the error is in the query you generate which contains at least 2 errors.
You fixed the first one (which is in my previous post: a comma before FROM).
The way to fix this kind of errors is to display the generated query (using dbms_output) before executing it and directly execute the displayed query in SQL*Plus, then you will see where the error is.

[Updated on: Wed, 10 December 2014 00:53]

Report message to a moderator

Re: ora-06550 [message #629503 is a reply to message #629465] Wed, 10 December 2014 00:50 Go to previous messageGo to next message
Shreya Aggarwal
Messages: 4
Registered: December 2014
Junior Member
@Michel Cadot

Thanks for the guidance.
I will follow your suggestions in future.

@Sanodani

ORA-06512: at "HR.EMP20", line 21


This mesage suggests that the error is in the package and not in the block where we are calling it.
Please check the package code for error.
Re: ora-06550 [message #629505 is a reply to message #629465] Wed, 10 December 2014 01:10 Go to previous messageGo to next message
sanodani
Messages: 98
Registered: October 2014
Member
Thank you all for your guidance and help.

I did find the error and now my program is working Smile
Re: ora-06550 [message #629506 is a reply to message #629505] Wed, 10 December 2014 01:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So post the final code for future readers.
Note that this is not the good way to return a result set as this consumes far more memory space and time than the good way which is to return a ref cursor.

[Updated on: Wed, 10 December 2014 01:12]

Report message to a moderator

Re: ora-06550 [message #629524 is a reply to message #629506] Wed, 10 December 2014 07:15 Go to previous messageGo to next message
sanodani
Messages: 98
Registered: October 2014
Member
Here is mine code.. Smile


Create or Replace Package test2 is
   
TYPE t_emptyptab is table of employees%ROWTYPE;

Type ref_cur is ref cursor;
    return employees%ROWTYPE;

Function getEmpNTab2 (pValue in Varchar2)             
    Return t_emptyptab;

End test2;
/

Create or Replace Package Body test2 is

Function getEmpNTab2 (pValue in Varchar2)             
    Return t_emptyptab
    is
    
    vSQL    Varchar2(2000);
    vTab    t_emptyptab;
Begin
    if pValue = 'oldtab' then
        vSQL    := 'SELECT e.empno employee_id, Null first_name, e.ename last_name '
                   ||      ', Null email, Null phone_number, e.hiredate hire_date '
                   ||      ', e.job job_id, e.sal salary '
                   ||      ', LEAST(ROUND(e.comm/e.sal,2),99/100) commission_pct '
                   ||      ', e.mgr manager_id, e.deptno department_id '
                   ||      ' FROM emp e ';
    elsif pValue = 'newtab' then
        vSQL    := 'SELECT e.* FROM employees e' ;
    end if;
    
    EXECUTE IMMEDIATE vSQL BULK COLLECT into vTab;
    --FOR i in vTab.first..vTab.last loop
            --pipe row (vTab(i));
        --end loop;
    Return vTab;
end getEmpNTab2;
  
end test2;
/


Declare 
    vTab test2.t_emptyptab  := test2.t_emptyptab();
    idx integer;
    
Begin
    vTab    := test2.getEmpNTab2('oldtab');
    idx := vTab.first;
    
    loop
        exit when idx = vTab.last;
            dbms_output.put_line(vTab(idx).employee_id || '  ' || vTab(idx).last_name||'  '|| vTab(idx).salary);
       idx  := vTab.next(idx);
    end loop;
end;

/
Re: ora-06550 [message #629530 is a reply to message #629524] Wed, 10 December 2014 08:28 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Ok this is good for an exercise about PL/SQL array but the good way to do it in reality is to:
1/ Not use dynamic query (execute immediate) as you have nothing dynamic
2/ To use ref cursor instead of PL/SQL array
Like (exception handler should be added to be complete):
SQL> Create or Replace Package test2 is
  2    procedure getEmpNTab2 (pValue in Varchar2, pCurs in out sys_refcursor);
  3  End test2;
  4  /

Package created.

SQL> sho err
No errors.
SQL> Create or Replace Package Body test2 is
  2    procedure getEmpNTab2 (pValue in Varchar2, pCurs in out sys_refcursor)
  3    is
  4    Begin
  5      if pValue = 'oldtab' then
  6        open pCurs for
  7          'SELECT e.empno employee_id, Null first_name, e.ename last_name '
  8          ||        ', Null email, Null phone_number, e.hiredate hire_date '
  9          ||      ', e.job job_id, e.sal salary '
 10          ||      ', LEAST(ROUND(e.comm/e.sal,2),99/100) commission_pct '
 11          ||      ', e.mgr manager_id, e.deptno department_id '
 12          ||      ' FROM emp e ';
 13      elsif pValue = 'newtab' then
 14        open pCurs for 'SELECT e.* FROM employees e' ;
 15      end if;
 16    end getEmpNTab2;
 17  end test2;
 18  /

Package body created.

SQL> sho err
No errors.
SQL> Declare
  2    vTab sys_refcursor;
  3    rec  employees%ROWTYPE;
  4  begin
  5    test2.getEmpNTab2('oldtab',vTab);
  6    dbms_output.put_line ('Old table:');
  7    loop
  8      fetch Vtab into rec;
  9      exit when vTab%notfound;
 10      dbms_output.put_line('  '||rec.employee_id||'  '||rec.last_name||'  '||rec.salary);
 11    end loop;
 12    close vTab;
 13    dbms_output.put_line ('New table:');
 14    test2.getEmpNTab2('newtab',vTab);
 15    loop
 16      fetch Vtab into rec;
 17      exit when vTab%notfound;
 18      dbms_output.put_line('  '||rec.employee_id||'  '||rec.last_name||'  '||rec.salary);
 19    end loop;
 20    close vTab;
 21  end;
 22  /
Old table:
  7369  SMITH  800
  7499  ALLEN  1600
  7521  WARD  1250
  7566  JONES  2975
  7654  MARTIN  1250
  7698  BLAKE  2850
  7782  CLARK  2450
  7788  SCOTT  3000
  7839  KING  5000
  7844  TURNER  1500
  7876  ADAMS  1100
  7900  JAMES  950
  7902  FORD  3000
  7934  MILLER  1300
New table:
  100  King  24000
  101  Kochhar  17000
  102  De Haan  17000
  103  Hunold  9000
  104  Ernst  6000
  105  Austin  4800
  106  Pataballa  4800
  107  Lorentz  4200
...
  204  Baer  10000
  205  Higgins  12008
  206  Gietz  8300

PL/SQL procedure successfully completed.

Re: ora-06550 [message #629537 is a reply to message #629530] Wed, 10 December 2014 08:56 Go to previous message
sanodani
Messages: 98
Registered: October 2014
Member
Thank you very much Michel Cadot Smile
Previous Topic: Can we get back any column after DROP
Next Topic: Scenario:
Goto Forum:
  


Current Time: Fri Apr 19 09:58:08 CDT 2024