2010-11-03 70 views
0

我已经使用了SQL函数来更新单个记录调用时:功能: 功能ToggleAnimalExclusion(AnimalId数量,StudyID数量)回数是SQL更新过程

PRAGMA AUTONOMOUS_TRANSACTION; 
exVal varchar2 (1); 

begin 
    select exclude 
    into exVal 
    from mbddx_study 
    where study_name = AnimalId and study_id = StudyID; 

if (exVal is null) then 
    update mbddx_study 
    set exclude = 'Y' 
    where study_name = AnimalId and study_id = StudyID ; 
else 
    update mbddx_study 
    set exclude = NULL 
    where study_name = AnimalId and study_id = StudyID ; 
end if ; 

commit; 
return 0; 
end ; 

从调用时这工作一个Perl脚本和单个数据库字段被更新。

现在,我想更新一组字段,使用与上面相同的结构,但每个study_name都是study_group的一部分。所以我想更新整个组,当组号传入(而不是study_name)时。

我的代码是:

function ToggleBoxExclusion (BoxId in number, StudyID in number) return number is 
    PRAGMA AUTONOMOUS_TRANSACTION; 
    exVal varchar2 (1); 
begin 
    select exclude 
    into exVal 
    from mbddx_animal 
    where box = BoxId and study_id = StudyID; 

    if (exVal is null) then 
     update mbddx_animal 
     set exclude = 'Y' 
     where box = BoxId and study_id = StudyID ; 
    else 
     update mbddx_animal 
     set exclude = NULL 
     where box = BoxId and study_id = StudyID ; 
    end if ; 

    commit; 
    return 0; 
end ; 

正如你所看到的,它是非常相似,我认为问题在于我试图更新多个字段。就目前来看,我在调用这个函数时没有更新字段。

任何想法?#

谢谢。

回答

0

我的第一个建议是使用where子句作为select来运行它,并确保返回任何记录。 (你有没有检查框和study_id值的功能?) 另外,如果你写的代码为:

if (exVal is null) then tempExclude := 'Y' else tempExclude := NULL; 
Update mbddx_animal 
set exclude = tempExclude where... 

它会更容易维护(一个更新语句而不是2)

+0

喜是的,当我将它作为select语句运行时,它们的返回状态良好。 – 2010-11-03 16:25:33

+0

然后下一步是尝试更新语句以外的语句。如果它与你的ecxpect出现在函数中的box/study一起工作,那么下一个我会做的就是在update语句之前打印它们的值。 此外,假设这是PLSQL,它返回0它应该是一个过程 – Joe 2010-11-03 16:49:10

+0

嗨,是的更新声明作为一个独立的工作正常。 – 2010-11-04 10:11:39