mysql - Transaction or variable declaration error -
i started learning transactions , stored procedures , i'm trying myself familiar different ways of assigning values result set variables.
what causing error on declare var_curcid int default 0;
here's i've done far.
create definer=`root`@`localhost` procedure `addcurriculumwithschoolyear`(in p_subjectname varchar(50), in p_yrlevel varchar(30), in p_desc text, in p_creator varchar(20)) begin declare haserror bool default 0; declare continue handler sqlexception set haserror = 1; declare var_curcid int default 0; declare var_systart int default 0; declare var_syend int default 0; start transaction; -- 1 insert statement curriculum table insert curriculum(`name`,`yearlevel`,`description`,`creator`) values(p_subjectname,p_yrlevel,p_desc,p_creator); -- 2 insert statement schoolyearcurriculum table insert schoolyearcurriculums(curriculumid,systart,syend) values(var_curcid,var_systart,var_syend); if `haserror` rollback; -- if error occurs of insert, select statements above, undo else commit; -- if insert, select statement successful, execute. select 'successfully inserted information'; end if; end;
the erorr on declaration of var_curcid
variable saying syntax error: unexpected 'var_curcid' (identifier)
i'm new hope can me. i'm still trying different ways such using :=
operator , select into
, set
.
i'd appreciate help.
thanks.
this bit tricky. put declarations before handler:
create procedure `addcurriculumwithschoolyear`(in p_subjectname varchar(50), in p_yrlevel varchar(30), in p_desc text, in p_creator varchar(20)) begin declare haserror bool default 0; declare var_curcid int default 0; declare var_systart int default 0; declare var_syend int default 0; declare continue handler sqlexception set haserror = 1; end
here sql fiddle.
in begin
/end
block, declare
s must first statements. handler after variable declarations; didn't realize required, however, until testing it.
Comments
Post a Comment