Not able to run large dynamic select query in stored procedure

自作多情 提交于 2020-01-06 07:42:06

问题


I have a stored procedure which is executing a dynamic select query. The query string is large. The following is the stored procedure


create or replace
procedure My_SP
(
procRefCursor out sys_refcursor,
--My other input variables here
)
is

dynSqlComplete varchar2(8000) := 'n/a';

begin

  dynSqlComplete := 'Large query here';

  open procRefCursor for dynSqlComplete;

end;

When I run this sp it shows the following error

ORA-00600: internal error code, arguments: [qcscbAddToSelLists], [], [], [], [], [], [], [], [], [], [], []

so I reduced the size of dynSqlComplete variable to varchar2(5000) and then ran the stored procedure. I got the following error:

ORA-06502: PL/SQL: numeric or value error: character string buffer too small

I have tried many things in vain and also I do not want to add them here because it will misguide.

-- EDIT -- 6 Jun 2012

Hi All,

I was able to pin point the problem but I am not yet able to solve it. I tried to run the query part by part and I found the query which was throwing an error. It contained START WITH and when i commented it the query started to work. I have given the code below and commented out the code which is giving error.


      SELECT RowNum AS RowNumber1,
      GR.*,
      --LEVEL AS LineageLvl,
      VDE.*
    FROM
      (SELECT *
      FROM group_relations  left outer join relation_classifier_instances RC on 
      rc.relation_id = group_relations.Group_relation_id  WHERE group_relation_type_id IN
      (19,20,32,38,42,43)  and (rc.relation_id is null) 
      ) GR
    LEFT OUTER JOIN Vendor_Feed_data_elements VDE
    ON GR.Group_Relation_Type_Id = 19
    AND GR.Primary_GroupField_Id = VDE.Vendor_Data_Element_Id
      /* Code which is giving the error
      START WITH
      (
        VDE.Vendor_Data_Element_Id IS NOT NULL  )
      CONNECT BY nocycle prior GR.RELATED_GROUPFIELD_ID = GR.PRIMARY_GROUPFIELD_ID*/


回答1:


You can try using a clob a CLOB in 11g, something like (untested):

declare
  l_sql clob;
  l_str1 varchar2(32767);
  l_str2 varchar2(32767);
begin
  dbms_lob.createtemporary(l_sql, false);

  l_str1 := 'some large SQL chunk';
  l_str2 := 'and the rest of large SQL chunk';

  l_sql := l_str1;
  dbms_lob.writeappend(l_sql, length(l_str2), l_str2);

  execute immediate l_sql;

  dbms_lob.freetemporary(l_sql);

end;



回答2:


Sure you don't want a function? Here's an example of something I made that's similar.

FUNCTION showbody(cust varchar2, receipt varchar2) 
    RETURN sys_refcursor AS retval sys_refcursor;
BEGIN
    OPEN retval FOR
        SELECT * 
        FROM comp.comp_remittance_details
        WHERE receipt_number=receipt 
            AND customer_number=cust;
    RETURN(retval);
END showbody;


来源:https://stackoverflow.com/questions/10803095/not-able-to-run-large-dynamic-select-query-in-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!