Pl / SQL将字符串分配给变量并每次打印值

时间:2017-08-17 10:30:31

标签: sql oracle plsql

创建一个名为' display_player_skill'的函数。它接受输入参数。输入参数是' player_id'数字作为数据类型。此函数应返回给定' player_id'的skil_id。

我曾尝试过

create or replace function display_player_skill (player_id in number)
    return varchar2
is
    skill_name varchar2(100);
begin
    select s.name into skill_name from k_skill s 
    inner join k_player p on p.skill_id=s.id
    where p.skill_id= player_id;

    case skill_name
        when 'Raider' then return 'Player is a Raider';
        when 'All rounder' then return 'Player is a All Rounder';
    end case;

exception
    when case_not_found then return 'Player is a Defender';
    when no_data_found then return 'No Such Player';
end;
/

但我必须返回给定player_id的skill_id并显示skill_name。通过将字符串赋值给变量并每次都打印该值。

2 个答案:

答案 0 :(得分:0)

似乎where子句不正确(将skill_id与player_id进行比较是很奇怪的)。

where p.skill_id= player_id;

此外,如果你在k_player中有列player_id,Oracle会将player_id视为列名。因此,您需要将参数命名为p_player_id(p_ for parameter)

所以我希望你的代码看起来像:

create or replace function display_player_skill (p_player_id in number)
    return varchar2
    is
    skill_name varchar2(100);
    skill_id   number;
    BEGIN
    select s.name, s.id into skill_name, skill_id from k_skill s 
    inner join k_player p on p.skill_id=s.id
    where p.player_id= p_player_id;

    dbms_output.put_line('Skill name is a '||skill_name);

    return skill_id;

EXCEPTION
when no_data_found then return 'No Such Player';
END;
/

我不知道您的表格结构,因此某些列名称可能需要更正。

答案 1 :(得分:0)

试一试:

 select s.name, s.id into skill_name,skill_id from k_skill s 
        inner join k_player p on p.skill_id=s.id
        where p.player_id= p_player_id;

  --      dbms_output.put_line('Skill name is a '||skill_name);

        return skill_id;