nemp表 id number(6) not null,name varchar2(20),hiredate date not null,job varchar2(10) not null,sal number(8,2),mgr number(6),deptno number(4)问题;建立函数get_sal,根据输入的雇员号返回雇员名和工资,不存在则显示消息“该雇员不存在”
我的答案;
create or replace function get_sal(eno number)
return varchar2,number
is
name nemp.name%type;
salary nemp.sal%type;
flag int;
begin
select count(id) into flag from nemp;
if flag>0 then
select sal into salary from nemp where id=eno;
return name,salary;
else
dbms_output.put_line('该雇员不存在');
end if;
end;
/
结果说带有编辑错误,

解决方案 »

  1.   

    return varchar2,number
    不能这样用吧?
    多个返回值的情况,可以使用out的参数来解决。
      

  2.   

    你说的out是BDMS_OUTPUT.PUT_LINE吗?
      

  3.   

    create or replace function get_sal(eno number)就是你的方法名里使用out作为返回参数,就不需要使用return返回了
      

  4.   

    --函数只能返回单个值,你这个要用过程
    SQL> ed
    已写入 file afiedt.buf  1  create or replace procedure
      2  tpro(v_no in emp.empno%type,v_name out varchar2,v_sal out number)
      3  as
      4  flag number;
      5  begin
      6  select count(*) into flag from emp where empno=v_no;
      7  if flag>0 then
      8  select ename,sal into v_name,v_sal from emp where empno=v_no;
      9  else
     10  dbms_output.put_line('该员工不存在');
     11  end if;
     12  exception
     13  when others then
     14  dbms_output.put_line('执行出错');
     15* end;
    SQL> /过程已创建。SQL> declare
      2  name varchar2(100);
      3  sal number;
      4  begin
      5  tpro('7369',name,sal);
      6  dbms_output.put_line(name||to_char(sal));
      7  end;
      8  /
    SMITH800PL/SQL 过程已成功完成。
      

  5.   

    不是,是函数的声明方式。
    create or replace function get_sal(eno number) return varchar2,number
    改为
    create or replace function get_sal(eno in number, sName out varchar2, sSalary out varchar2) return number
      

  6.   

    现在的;
    create or replace function get_sal(eno number,name out varchar2,salary out number)
    return number
    is
    name nemp.name%type;
    salary nemp.sal%type;
    flag int;
    begin
    select count(id) into flag from nemp;
    if flag>0 then
    select sal into salary from nemp where id=eno;
    else
    dbms_output.put_line('该雇员不存在');
    end if;
    end;
    /
    还是有编辑错误,
      

  7.   

    我试了下,注释掉和表相关的语句能够通过,说明结构上没有问题了。你再调试下吧,先函数内所有语句注释掉只保留一个return,然后一句一句增加。
      

  8.   

    create or replace function get_sal(eno number,name out varchar2)
    return number
    is
    salary nemp.sal%type;
    flag int;
    begin
    select count(id) into flag from nemp;
    if flag>0 then
    select sal into salary from nemp where id=eno;
    return salary;
    else
    dbms_output.put_line('该雇员不存在');
    end if;
    end;
    /新的,没有编辑错误,但调用不行,你试下,
      

  9.   

    你指的调用是在SQL语句里调用吗?带有out参数的是不能直接用在SQL里的。
      

  10.   

    直接使用子查询的SQL语句吧,别使用函数了
      

  11.   

    create or replace function get_sal(eno number) return varchar2
    is
    flag int;
    name_sal varchar2(50);
    begin
    select count(id) into flag from nemp;
    if flag>0 then
    select name||','||sal into name_sal from nemp where id=eno;
    return name_sal;
    else
    dbms_output.put_line('该雇员不存在');
    end if;
    end;--
    select get_sal('员工号') from nemp
      

  12.   

    create or replace function get_sal(eno number) return varchar2
    is
    flag int;
    name_sal varchar2(50);
    begin
    select count(id) into flag from nemp where id=eno;  --where id=eno加个这个
    if flag>0 then
    select name||','||sal into name_sal from nemp where id=eno;
    return name_sal;
    else
    dbms_output.put_line('该雇员不存在');
    end if;
    end;