如果表

时间:2017-12-04 10:43:07

标签: stored-procedures plsql

我正在尝试创建应该一次返回单个值的过程。实际上,我已经做了,但我面临着一个挑战。

假设我正在向程序传递3个输入,并且根据这些输入过程将返回单个输出。但是我所引用的表包含更多针对该特定查询的结果,而我的过程正在向我返回multiplestate异常。

在这种情况下,有谁可以帮助我。以下是我正在使用的程序。

PROCEDURE p_regidexport(countryid IN varchar2, cropid IN varchar2, productid IN VARCHAR2, pregid out varchar2)
IS
fnc       VARCHAR2(30) := 'P_REGIDEXPORT';
query     VARCHAR2(10000);
regid varchar2(20);


BEGIN
        begin
          select nvl(REG_ID,'0') into regid from Registration 
          where 
          LOC_ID =(select loc_id from location where Country = countryid ) AND 
          CROP_ID = (select crop_id from crop where CROP_NM =  cropid)AND 
          REG_NAME = productid ;
          EXCEPTION
            WHEN no_data_found THEN
                  dbms_output.put_line('No record ');  --- or regid  ='0';  
        end;
        pregid := regid;
        --sub_log('P_REGIDEXPORT:'||pregid);
        dbms_output.put_line(pregid);
        EXCEPTION
        WHEN others THEN
        dbms_output.put_line('No record present' || ' - ' || sqlerrm);      
    END P_REGIDEXPORT;

1 个答案:

答案 0 :(得分:0)

Chahar,根据您的要求,您可以使用管道功能。在此,您可以调用该函数,就像调用表一样。请在下面找到示例代码,以帮助您理解这个概念。

示例代码如下:

CREATE OR REPLACE 
PACKAGE TEST_PIPELINE AS 

type return_varchar is table of varchar2(4000);

  /* TODO enter package declarations (types, exceptions, methods etc) here */ 

FUNCTION row_generator return return_varchar PIPELINED ;
END TEST_PIPELINE;


CREATE OR REPLACE
PACKAGE BODY TEST_PIPELINE AS

  FUNCTION row_generator return return_varchar PIPELINED  AS
  BEGIN
    -- TODO: Implementation required for FUNCTION TEST_PIPELINE.row_generator
   for i in  (select col1 from(
select 1 as col1 from dual
union all
select 2 from dual
union all
select 3 from dual)) loop
    pipe row(i.col1);
end loop;



  END row_generator;

END TEST_PIPELINE;

调用管道功能:<​​/ p>

select * from table(TEST_PIPELINE.row_generator);

我修改了你的代码,请看一下:

 CREATE OR REPLACE 
PACKAGE regdexport AS 

type return_varchar is table of varchar2(4000);


FUNCTION p_regidexport(countryid IN varchar2, cropid IN varchar2, productid IN VARCHAR2, pregid out varchar2) return return_varchar PIPELINED ;
END regdexport;


CREATE OR REPLACE
PACKAGE BODY regdexport AS
FUNCTION p_regidexport(countryid IN varchar2, cropid IN varchar2, productid IN VARCHAR2, pregid out varchar2) return return_varchar
IS
fnc       VARCHAR2(30) := 'P_REGIDEXPORT';
query     VARCHAR2(10000);
regid varchar2(20);


BEGIN
        begin
          for  i in (select nvl(REG_ID,'0') into regid from Registration 
          where 
          LOC_ID =(select loc_id from location where Country = countryid ) AND 
          CROP_ID = (select crop_id from crop where CROP_NM =  cropid)AND 
          REG_NAME = productid) 
          loop
                pipe row(i.regid);
          end loop;
          EXCEPTION
            WHEN no_data_found THEN
                  dbms_output.put_line('No record ');  --- or regid  ='0';  
        end;
        EXCEPTION
        WHEN others THEN
        dbms_output.put_line('No record present' || ' - ' || sqlerrm);      
    END P_REGIDEXPORT;

调用该函数:

select * from table(regdexport.p_regidexport(par1,par2,par3));

希望这有帮助。