函数oracle中的变量声明

时间:2014-10-21 15:58:08

标签: oracle plsql oracle11g oracle10g toad

这个功能有什么问题吗?它抛出无效的sql语句错误。 我是oracle的新手

create or replace function get_manager_names_str
(
   p_list in varchar2 := '793245567,65177897654'
)return varchar2 is
mgr_name varchar2(32767);

declare   -- i tried to remove the declare but still not working am i doing any thing wrong ?
mgr_id manager.manager_id%TYPE;
cursor split_values is 
select column_value from table (split_manager_list(p_list));  -- for testing you can test with                                                                                                                                                                     ---  some value

cursor manager_names is 
select * from manager 
where manager_id= mgr_id;

begin 
for r_split_values in split_values
loop
mgr_id := r_split_values.column_value;
dbms_output.put_line('splitvalue :'|| r_split_values.column_value);
for r_manager_names in manager_names
loop
mgr_name := mgr_name||r_manager_names.first_name || r_manager_names.last_name || r_manager_names.middle_name||'. ,' ;
end loop;
end loop;
dbms_output.put_line(mgr_name);
return mgr_name;
end; 
show errors;

任何帮助将不胜感激。感谢

补充:split_list函数用于分割逗号分隔值

Create or replace type split_tbl as table of varchar2(32767);
create or replace function split_manager_list
(
    p_list varchar2 ,
    p_del varchar2 := ','
) return split_tbl pipelined
is
    l_idx    pls_integer;
    l_list    varchar2(32767) := p_list;
    l_value    varchar2(32767);
begin
    loop
        l_idx := instr(l_list,p_del);
        if l_idx > 0 then

            pipe row( substr(l_list,1,l_idx-1));
            l_list := substr(l_list,l_idx+length(p_del));

        else
            pipe row( l_list);
            exit;
        end if;
    end loop;
    return;
end split_manager_list;
Show errors;

1 个答案:

答案 0 :(得分:2)

create or replace function get_manager_names_str (p_list in varchar2 default '793245567,65177897654') return varchar2 
is
  mgr_name varchar2(32767);
  mgr_id manager.manager_id%TYPE;
begin 
  for r_split_values in (select column_value from table (split_manager_list(p_list))) loop
    mgr_id := r_split_values.column_value;
    dbms_output.put_line('splitvalue :' || r_split_values.column_value);
    for r_manager_names in (select * from manager where manager_id = mgr_id) loop
      mgr_name := mgr_name || r_manager_names.first_name || r_manager_names.last_name || r_manager_names.middle_name || '. ,';
    end loop;
  end loop;
  dbms_output.put_line(mgr_name);
  return mgr_name;
end; 
  • 使用隐式游标

  • 已移除声明

  • 已替换:=使用DEFAULT

  • 重新格式化(更容易阅读和发现错误)

我发现的唯一问题是DECLARE + DEFAULT