9

我尝试在我的 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 Developerper 中清楚地表明我的 Select 不让他满意,并且无法弄清楚原因。我阅读的 Oracle 库、论坛和代码示例没有多大帮助。

4

2 回答 2

17

你需要使用SELECT INTO. 此外,PL/SQL您不需要使用大括号THENand 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;
于 2013-04-28T08:04:32.170 回答
3

我不认为你可以为这样的变量赋值。像这样尝试 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;
于 2013-04-28T07:13:40.487 回答