sql更新过程
我使用SQL函数在调用时更新单个记录:函数为: 函数ToggleAnimalExclusion AnimalId in number,StudyID in number返回数为sql更新过程,sql,Sql,我使用SQL函数在调用时更新单个记录:函数为: 函数ToggleAnimalExclusion AnimalId in number,StudyID in number返回数为 PRAGMA AUTONOMOUS_TRANSACTION; exVal varchar2 (1); begin select exclude into exVal from mbddx_study where study_name = AnimalId and study_id =
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脚本调用并且更新单个数据库字段时,这种方法可以工作
现在,我想更新一组字段,使用与上面相同的结构,但每个研究名称都是研究组的一部分。所以我想更新整个组,当传入组号而不是研究名称时
我的密码是:
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 ;
正如你所看到的,这是非常相似的,我认为问题在于我试图更新一些字段。目前,当我调用此函数时,没有字段被更新
有什么想法吗
谢谢。我的第一个建议是将其作为带有where子句的select运行,并确保返回所有记录。您是否在函数中选中了box和study_id的值? 此外,如果您将代码编写为:
if (exVal is null) then tempExclude := 'Y' else tempExclude := NULL;
Update mbddx_animal
set exclude = tempExclude where...
维护一个update语句而不是2个update语句会更容易。您好,是的,当我将其作为select语句运行时,它们将被很好地返回。然后,下一步是尝试函数之外的update语句。如果它与您希望出现在函数中的框/研究一起工作,那么我要做的下一件事就是在update语句之前打印它们的值。另外,假设这是PLSQL,并且它返回0,那么它应该是一个过程。语句不在某种循环中是否重要?您如何调用该函数?您是否已从函数中打印出参数,以查看它们的设置?为什么这是作为一个自治事务进行的?为什么允许编译器为您执行隐式数据类型转换?返回值应为“0”,因为返回类型为varchar2。而且,因为您只返回一个字符,所以返回类型应该是Char。但是,正如我之前所说的,这应该被编码为一个过程,而不是一个函数。无论如何,谢谢你的建议。