Note that there are some explanatory texts on larger screens.

plurals
  1. POAttempting to create variable variables in mysql ONLY. (Not using PHP)
    text
    copied!<h2>Context</h2> <p>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.</p> <p>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.</p> <p>So then later on in the .sql script where you see two lines like these: </p> <pre><code>INSERT INTO relatedTable (col1,col2,specialColumn,col3,col4) VALUES ('','',@autoIncrementColumnName,'',''); INSERT INTO relatedTable (col1,col2,specialColumn,col3,col4) VALUES ('','',@autoIncrementColumnName,'',''); </code></pre> <p>It needs to insert the mysql value it stored earlier but all of the variables are being overwritten except for one.</p> <h2>Two Questions</h2> <ol> <li><p>Is it possible to create variable variables using only MYSQL? Like this: </p> <pre><code>SET @dynamicVarName = CONCAT('guestCreditCardId', LAST_INSERT_ID()); SET @@dynamicVarName = LAST_INSERT_ID(); </code></pre></li> <li><p>If variable variables are not possible, what solution could I use?</p></li> </ol> <h2>Thank you so much!</h2>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload