SQL更新程序

时间:2010-11-03 16:12:55

标签: sql

我在调用时使用了SQL函数来更新单个记录:该函数是: function ToggleAnimalExclusion(NumberI中的AnimalId,number中的StudyID)返回数是

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 ;

正如您所看到的,它非常相似,我认为问题在于我尝试更新许多字段。按照目前的情况,当我调用此函数时,不会更新任何字段。

任何想法?#

感谢。

1 个答案:

答案 0 :(得分:0)

我的第一个建议是使用where子句将其作为select运行,并确保返回任何记录。 (你在函数中检查了box和study_id的值吗?) 另外,如果您将代码编写为:

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

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