2010-11-03 71 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