功能实现:
从表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;结果是空?为什么,再查询不到也得有数据才行啊...
从表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;结果是空?为什么,再查询不到也得有数据才行啊...
into result
from
(select doc_id from declarant_history
union
select doc_id from declarant_information
)
where cop_code=p_cop_code;
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 get_docid('111111111') as doc_id from dual;
结果为空啊!
单独的select seq_doc.nextval from dual是不空的。
union 相当于distinct了
谢谢,不过我这样的查询结果还是空,不知道是为什么
select get_docid('111111111') as doc_id from dual;
除了这样查询查询,有没有其它办法?
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;
你好,我的用法有误,我的原想法,如果A找不到记录,就从B找,再没有的从序列中找。
我以前NVL可以发现空记录,想不到NVL只是查找NULL记录。
所以,怎样判断查询空记录?
等待别人来解答吧
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;我把上面那句注释掉,就可以了,不注释,发现什么值都没有,郁闷。
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;
你这样就可以了,郁闷。按道理没有no_data_found的话,应该最后一句就执行啊,它不出错,也不提示。
终于解决了,谢谢你。
难道是有exception,但没有触发?
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;