表名是传过去的 tab,列名也是传过去的 sell
create or replace function auto_nid(tab in varchar2,sell in varchar2) return varchar2
is
inx varchar2(20);
iny number;
inz number;
revalues varchar2(20);
begin
inx:=1;
inz:=1;
select count(*) into iny from tab;
if (iny is null or iny=0 ) then
return iny;
else
while to_number(inx)<=iny loop
begin
select count(*) into inz from tab where sell=inx;
if inz=1 then
inx:=to_number(inx)+1;
revalues:=inx;
else
exit;
end if;
end;
end loop;
end if;
return revalues;
end;
用动态语句吧。
execute immediate into
create or replace function auto_nid(tab in varchar2,sell in varchar2) return varchar2
is
inx varchar2(20);
iny number;
inz number;
revalues varchar2(20);
v_sql varchar2(4000);
begin
inx:=1;
inz:=1;
v_sql:=' select count(1) from '||tab;
execute immediate v_sql into iny;
if iny>0 then
while to_number(inx)<=iny loop
v_sql:=' select count(1) from '||tab ||' where sell='||inx;
execute immediate v_sql into inz;
end loop;
end if;
end;