Given a book number id and the number of book copies, I want to insert as many rows into a table as there are book copies. This is my non-working solution:
DROP PROCEDURE IF EXISTS insert_into_book_copy_table;
CREATE PROCEDURE insert_into_book_copy_table (IN in_book_id INT,
IN in_num INT)
BEGIN
SET @I = 1;
SET @ACQUIRED = CURRENT_TIMESTAMP;
WHILE I <= in_num DO
INSERT INTO book_copy (book_id, acquired) # The table has an
VALUES (in_book_id, @ACQUIRED); # auto-increment field
# which I didn't list here
SET @I = @I + 1;
END WHILE;
END$$
For instance, if the given book number id is 23 and the number of book copies is equal to 8 then the book_copy table should look like this:
SELECT * FROM book_copy;
╔══════════════╦══════════╦═════════════════════╗
║ book_copy_id ║ book_id ║ acquired ║
╠══════════════╬══════════╬═════════════════════╣
║ 1 ║ 23 ║ 2013-04-15 18:15:20 ║
║ 2 ║ 23 ║ 2013-04-15 18:15:20 ║
║ 3 ║ 23 ║ 2013-04-15 18:15:20 ║
║ 4 ║ 23 ║ 2013-04-15 18:15:20 ║
║ 5 ║ 23 ║ 2013-04-15 18:15:20 ║
║ 6 ║ 23 ║ 2013-04-15 18:15:20 ║
║ 7 ║ 23 ║ 2013-04-15 18:15:20 ║
║ 8 ║ 23 ║ 2013-04-15 18:15:20 ║
╚══════════════╩══════════╩═════════════════════╝
And is there a better way of coding this?