mysql - Error Code 1064 for BEGIN Try -
i need trying figure out why begin try throwing me error?
use my_guitar_shop; drop procedure if exists update_product_discount; delimiter $$ create procedure update_product_discount ( in product_id int, in discount_percent int ) begin begin try update products set discount_percent = discount_percent product_id = product_id end try; end; begin catch if discount_percent < 0 select 'the discount percent must positive' message end catch; end $$ delimiter ;
you should test discount_percent before try update. unless there's problem update, won't fail , see catch.
also, don't use same names variables column names.
use my_guitar_shop; drop procedure if exists update_product_discount; delimiter $$ create procedure update_product_discount ( in this_product_id int, in new_discount_percent int ) begin if discount_percent < 0 begin select 'the discount percent must positive' message; end else begin begin try update products set discount_percent = new_discount_percent product_id = this_product_id; end try begin catch select 'an error occured while saving.' message; end catch end end end $$ delimiter ;
Comments
Post a Comment