I must be overlooking something simple. I’m setting a variable from a query result in a MySQL stored procedure like this:
SELECT @myName := username FROM User WHERE ID=1;
So, @myName is storing the username of userid 1, which is ‘Paul’. Great.
But later in the stored procedure I run an update on this record:
UPDATE User SET username = 'Fred' WHERE ID=1;
Now, for some reason, @myName = ‘Fred’ when it should still equal ‘Paul’, right? It appears MySQL is just creating a pointer to the record rather than storing a static value in the @myName variable.
So in short, I want to be able to store a value in a variable from a query result and assure the value of my variable doesn’t change, even when the data in the table it was set from changes.
What am I missing? Thanks in advance!
That’s very surprising, I agree. I’m not sure how to explain it, but for what it’s worth, try this instead:
See http://dev.mysql.com/doc/refman/5.0/en/select-into-statement.html
update: I’m trying to reproduce this problem, but I can’t. I’m using MySQL 5.0.51 on Mac OS X.
Can you try the code above in your test database and let us know if it exhibits the problem you describe?