我尝试在Oracle数据库中创建一个过程,但无法实现将查询结果分配给我的变量。这是我正在尝试调试的内容:
create or replace
PROCEDURE PCD_COMBAT (identifier_perso NUMBER, identifier_advers NUMBER) AS
ATT_PERSO NUMBER;
OFF_PERSO NUMBER;
DEF_ADVERS NUMBER;
BEGIN
OFF_PERSO := SELECT OFFENSE_PERSO FROM PERSONNAGE WHERE ID_PERSO = identifier_perso;
DEF_ADVERS := SELECT DEFENSE_ADVERSAIRE FROM PERSONNAGE WHERE ID_ADVERSAIRE = identifier_advers;
ATT_PERSO := OFF_PERSO - DEF_ADVERS;
IF ATT_PERSO < 1 { ATT_PERSO := 1 };
END PCD_COMBAT;
在Oracle Developper中明确指出,我的Select并没有让他高兴,也无法弄明白为什么。我阅读的Oracle库,论坛和代码示例没有多大帮助。
答案 0 :(得分:12)
您需要使用SELECT INTO
。 PL/SQL
中还没有大括号需要使用THEN
和END IF
。我不太清楚你对结果做了什么。你想要还给它吗?那你需要一个FUNCTION
。它应该看起来像这样(未经测试):
create or replace
FUNCTION PCD_COMBAT (identifier_perso NUMBER, identifier_advers NUMBER)
RETURN NUMBER
AS
ATT_PERSO NUMBER;
OFF_PERSO NUMBER;
DEF_ADVERS NUMBER;
BEGIN
SELECT OFFENSE_PERSO
INTO OFF_PERSO
FROM PERSONNAGE
WHERE ID_PERSO = identifier_perso;
SELECT DEFENSE_ADVERSAIRE
INTO DEF_ADVERS
FROM PERSONNAGE
WHERE ID_ADVERSAIRE = identifier_advers;
ATT_PERSO := OFF_PERSO - DEF_ADVERS;
IF ATT_PERSO < 1 THEN
ATT_PERSO := 1;
END IF
RETURN ATT_PERSO;
END PCD_COMBAT;
答案 1 :(得分:2)
我认为你不能为这样的变量赋值。尝试使用SELECT INTO:
create or replace
PROCEDURE PCD_COMBAT (identifier_perso NUMBER, identifier_advers NUMBER) AS
ATT_PERSO NUMBER;
OFF_PERSO NUMBER;
DEF_ADVERS NUMBER;
BEGIN
SELECT OFFENSE_PERSO INTO OFF_PERSO FROM PERSONNAGE WHERE ID_PERSO = identifier_perso;
SELECT DEFENSE_ADVERSAIRE INTO DEF_ADVERS FROM PERSONNAGE WHERE ID_ADVERSAIRE = identifier_advers;
ATT_PERSO := OFF_PERSO - DEF_ADVERS;
IF ATT_PERSO < 1 { ATT_PERSO := 1 };
END PCD_COMBAT;