Context
I have a dynamically generated .sql file that contains a series of insert statements which are going to be used for inserting data into many different tables that are dependent on other tables.
After each insert statement is ran, if a particular table has an autoincremented id column, then the text "SET @autoIncrementColumnName = LAST_INSERT_ID();" is generated which stores the last insert id of that insert statement in a mysql variable. Then if there is another INSERT statement for that particular table, the process is repeated. The problem is that each statement "SET @autoIncrementColumnName = LAST_INSERT_ID();" overwrites the previous variable before it is able to use the variable later on in the .sql file.
So then later on in the .sql script where you see two lines like these:
INSERT INTO relatedTable (col1,col2,specialColumn,col3,col4) VALUES ('','',@autoIncrementColumnName,'','');
INSERT INTO relatedTable (col1,col2,specialColumn,col3,col4) VALUES ('','',@autoIncrementColumnName,'','');
It needs to insert the mysql value it stored earlier but all of the variables are being overwritten except for one.
Two Questions
Is it possible to create variable variables using only MYSQL? Like this:
SET @dynamicVarName = CONCAT('guestCreditCardId', LAST_INSERT_ID()); SET @@dynamicVarName = LAST_INSERT_ID();
If variable variables are not possible, what solution could I use?