DELIMITER $$ CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`copyComponentsFromSaleList`(vTargetTicketFk INT) BEGIN /* Copy sales and components to the target ticket * * ¡¡¡¡ Requires tmp.saleList(saleFk, itemFk, quantity, concept, price, discount, orden) !!!!! * * param VTargetTicketFk id del ticket a rellenar */ INSERT INTO vn.sale(ticketFk, itemFk, quantity,concept,price,discount) SELECT vTargetTicketFk, itemFk, quantity,concept,price,discount FROM tmp.saleList ORDER BY orden; SET @order = 0; DROP TEMPORARY TABLE IF EXISTS tNewSaleList; CREATE TEMPORARY TABLE tNewSaleList SELECT id as saleFk, @order := @order + 1 as orden FROM vn.sale WHERE ticketFk = vTargetTicketFk ORDER BY saleFk; INSERT INTO vn.saleComponent(saleFk,componentFk,value) SELECT ns.saleFk, sc.componentFk, sc.value FROM vn.saleComponent sc JOIN tmp.saleList s ON s.saleFk = sc.saleFk JOIN tNewSaleList ns ON ns.orden = s.orden; DROP TEMPORARY TABLE tNewSaleList; END$$ DELIMITER ;