MySQL Procedure does not compile - Error on delete with variable -
i'm writing first procedure , ge error. i've reduced error delete line unsure why. can spot issue here? variable?
drop procedure if exists mpt_proc; delimiter $$ create procedure mpt_proc modifies sql data begin #-- declare statements declare v_user_id int default 0; declare no_more_rows boolean; declare v_loop_cntr int default 0; declare v_num_rows int default 0; declare c_userfiles cursor select distinct f.user_id mpt_stg_fileupload f f.status = 'a'; #-- accepted declare continue handler not found set no_more_rows = true; open c_userfiles; #-- loop through each user_id found pending the_loop: loop fetch c_userfiles v_user_id; #-- break out of loop if #-- 1) there no records, or #-- 2) we've processed them all. if no_more_rows close c_userfiles; leave the_loop; end if; delete mpt_stg_fileupload s s.user_id = v_user_id; commit; #--commiting changes user end loop the_loop; end if; end delimiter ;
mysql doesn't allow provide alias table deleting in delete statement.
delete mpt_stg_fileupload user_id = v_user_id;
and lose pound signs comment delimiters. double dash standard marking beginning of comment. looks have unmatched end if
towards end of procedure.
drop procedure if exists mpt_proc; delimiter $$ create procedure mpt_proc modifies sql data begin declare v_user_id int default 0; declare no_more_rows boolean; declare v_loop_cntr int default 0; declare v_num_rows int default 0; declare c_userfiles cursor select distinct f.user_id mpt_stg_fileupload f f.status = 'a'; -- accepted declare continue handler not found set no_more_rows = true; open c_userfiles; -- loop through each user_id found pending the_loop: loop fetch c_userfiles v_user_id; -- break out of loop if -- 1) there no records, or -- 2) we've processed them all. if no_more_rows close c_userfiles; leave the_loop; end if; delete mpt_stg_fileupload s s.user_id = v_user_id; commit; --commiting changes user end loop the_loop; end$$ delimiter ;
if user_id
not primary key on mpt_stg_fileupload table, procedure deleting rows not marked "accepted". if user_id has 2 rows in table, , 1 of rows marked accepted, , other not. did want delete both rows?
it looks you've got 2 variables declared aren't being referenced anywhere, v_loop_cntr
, v_num_rows
, i'd recommend comment them out if aren't needed.
this entire procedure done more efficiently in single sql statement.
Comments
Post a Comment