功能实现:
    从表A中查询一个doc_ID(怎样保证是唯一值?)如无记录,从表B中查询(也得保证唯一性),如再没有的话,从序列sequence中获得一个序列;
函数代码:create or replace function get_DocID(p_cop_code in varchar2) return varchar2 is
  Result varchar2(10);
begin
  result:='0';
  --档案号先从历史记录中查找
  if result='0' then
    select nvl(doc_id,'0')  into result from declarant_history where cop_code=p_cop_code;
  end if;
  --历史中如无档案号,到当前信息表中查询
  if result='0' then
    select nvl(doc_id,'0') into result  from declarant_information where cop_code=p_cop_code;
  end if;
  --实在没有,由SQL的序列生成一个
  if result='0' then
    select seq_doc.nextval into result from dual;
  end if;
  
  return(Result);
end get_DocID;函数应用:
我不知道怎么应用函数,只好这样?(不知道有没有更好的办法?)
select get_docid('111111111') as doc_id from dual;结果是空?为什么,再查询不到也得有数据才行啊...

解决方案 »

  1.   

    根据楼主的需求,应该可以用CASE WHEN来解决
      

  2.   

    select nvl(doc_id,seq_doc.nextval ) 
    into result  
    from 
    (select doc_id from declarant_history
    union 
    select  doc_id from declarant_information
    )
    where cop_code=p_cop_code; 
      

  3.   

    select nvl(doc_id,seq_doc.nextval ) 
    into result  
    from 
    (select doc_id,cop_code from declarant_history
    union 
    select  doc_id,cop_code from declarant_information
    )
    where cop_code=p_cop_code; 
      

  4.   

    你好,我要唯一值呢,如果这样用union,会不会有多行?
      

  5.   

    如果存在多行呢?要不要distinct下?
      

  6.   

    如果这个union存在多行的话,插入result算哪个值呢?
    我刚才按你说的这个做,发现
    select get_docid('111111111') as doc_id from dual;
    结果为空啊!
    单独的select seq_doc.nextval from dual是不空的。
      

  7.   

    union all不去重
    union 相当于distinct了
      

  8.   


    谢谢,不过我这样的查询结果还是空,不知道是为什么
    select get_docid('111111111') as doc_id from dual;
    除了这样查询查询,有没有其它办法?
      

  9.   

    先看看有输出结果吗?
    create or replace function get_DocID(p_cop_code in varchar2) return varchar2 is 
      Result varchar2(10); 
    begin 
      result:='0'; 
      --档案号先从历史记录中查找 
      if result='0' then 
        select nvl(doc_id,'0')  into result from declarant_history where cop_code=p_cop_code; 
        dbms_output.put_line(result);
      end if; 
      --历史中如无档案号,到当前信息表中查询 
      if result='0' then 
        select nvl(doc_id,'0') into result  from declarant_information where cop_code=p_cop_code; 
        dbms_output.put_line(result);
      end if; 
      --实在没有,由SQL的序列生成一个 
      if result='0' then 
        select seq_doc.nextval into result from dual; 
        dbms_output.put_line(result);
      end if; 
      
      return(Result); 
    end get_DocID; 
      

  10.   


    你好,我的用法有误,我的原想法,如果A找不到记录,就从B找,再没有的从序列中找。
    我以前NVL可以发现空记录,想不到NVL只是查找NULL记录。
    所以,怎样判断查询空记录?
      

  11.   

    这个,我觉得你的那个过程和3楼的sql应该都能成功的实现你的需求才对,但事实却不是这样,这个我也很奇怪,不太清楚。
    等待别人来解答吧
      

  12.   

    create or replace function get_DocID(p_cop_code in varchar2) return varchar2 is
      Result varchar2(10);
    begin
     /* select nvl(doc_id,seq_doc.nextval )into result  
    from 
        (select doc_id,cop_code from declarant_history
        union 
        select  doc_id,cop_code from declarant_information
        )
    where cop_code=p_cop_code; */
    select to_char(seq_doc.nextval) into result from dual;
    return(Result);
    end get_DocID;我把上面那句注释掉,就可以了,不注释,发现什么值都没有,郁闷。
      

  13.   

    你试试这个可以吗
    create or replace function get_DocID(p_cop_code in varchar2) return varchar2 is
      Result varchar2(10);
    begin
      select doc_id into result  
      from 
        (select doc_id,cop_code from declarant_history
        union 
        select  doc_id,cop_code from declarant_information
        )
      where cop_code=p_cop_code; 
      return(Result);
    exception
      when no_data_found then
      select to_char(seq_doc.nextval) into result from dual;
      return(Result);
    end get_DocID;
      

  14.   


    你这样就可以了,郁闷。按道理没有no_data_found的话,应该最后一句就执行啊,它不出错,也不提示。
    终于解决了,谢谢你。
    难道是有exception,但没有触发?
      

  15.   

    正解:CREATE OR REPLACE FUNCTION GET_DOCID(P_COP_CODE IN VARCHAR2) RETURN VARCHAR2 IS
      RESULT VARCHAR2(10);
    BEGIN
      SELECT NVL(DOC_ID,SEQ_DOC.NEXTVAL) INTO RESULT  
      FROM 
        (SELECT DOC_ID,COP_CODE FROM DECLARANT_HISTORY
        UNION 
        SELECT  DOC_ID,COP_CODE FROM DECLARANT_INFORMATION
        )
      WHERE COP_CODE=P_COP_CODE; 
      RETURN(RESULT);
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
      SELECT TO_CHAR(SEQ_DOC.NEXTVAL) INTO RESULT FROM DUAL;
      RETURN(RESULT);
    END GET_DOCID;
      

  16.   

    你的返回变量是关键字吧!Result 你换个名字试试。