Q: How to do a procedure stored in MySQL that inserts the ID from one table to another? How to do a procedure stored in MySQL that inserts the ID from one table to another?
How to make a stored procedure in mysql that allows to validate if a repeated data is found and if so to be able to register in two related tables otherwise insert only the id in a table?
I have the following SQL script works if cc is not found in the database and inserted into the two tables but if the cc variable is in the DB it does not work Thanks
DROP PROCEDURE IF EXISTS `owners_add`;
DELIMITER $$
CREATE PROCEDURE owners_add(
IN `@name` varchar(35),
IN `@last` varchar(35),
IN `@cc` int(12),
IN `@cell_phone` varchar(15))
BEGIN
  DECLARE id_people INT DEFAULT 0;
  SET id_people=(SELECT id  FROM people WHERE cc=@cc);
  IF(id_people IS NULL) THEN
     INSERT INTO people(name,last,cc,cell_phone) 
                VALUES(`@name`,`@last`,`@cc`,`@cell_phone`);
     SET @ID = LAST_INSERT_ID();
     INSERT INTO owners(id) VALUES(@ID);
  ELSE
     INSERT INTO owners(id) VALUES(id_people);
  END IF; 
  END$$


A: you can set last insert id and then use that last insert id as value into your 2nd table like ..
set o_maxId=(select LAST_INSERT_ID());
insert into your second tablename (column name,.,.,.,)values(value1.,.,o_maxId);
