I'm having trouble using PREPARE and EXECUTE as I'm doing something a little odd - executing a pre-prepared SQL statement taken from a database (rather than building it in the Stored Procedure).
I saw the question MySQL :: Run SQL statement within Variable and this one My SQL Dynamic query execute and get ouput into a variable in stored procedure and from these gathered that I cannot use a SP DECLARED variable in the prepare statement (MySQL WorkBench gives an error as soon as I try) and so have to use a variable with 'AT symbol' (not sure what this means?). So it is I have been trying to get the following to work:
DECLARE sqlUpdateSp TEXT;
IF (SELECT EXISTS (SELECT updateSql FROM sys_instantUpdates WHERE expiryDate >= NOW() AND securityToken = securityTokenIn AND updateType = updateTypeIn)) THEN
SELECT updateSql INTO sqlUpdateSp FROM sys_instantUpdates WHERE expiryDate >= NOW() AND securityToken = securityTokenIn AND updateType = updateTypeIn;
SET @phlanx = sqlUpdateSp;
PREPARE sqlToExecute FROM @phlanx;
EXECUTE sqlToExecute;
END IF
I get no errors and the rest of the SP functions fine but this SQL never gets executed, I'm guessing because 'sqlToExecute' isn't being formed properly.
Everything I can find on PREPARE/EXECUTE in SPs assumes that the SQL being executed is being created in the SP, not pulled from a DB.