Dynamic MySQL with local variables
问题 How can I use dynamic SQL statements in MySQL database and without using session variables? Right now I have such a code (in MySQL stored procedure): (...) DECLARE TableName VARCHAR(32); SET @SelectedId = NULL; SET @s := CONCAT("SELECT Id INTO @SelectedId FROM ", TableName, " WHERE param=val LIMIT 1"); PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt; IF ISNULL(@SelectedId) THEN (...) But I'd like to use only local variables, that means I'd like to start this procedure with: