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:
DECLARE TableName VARCHAR(32); DECLARE s VARCHAR(1024); DECLARE SelectedId INTEGER UNSIGNED; (...)
and do not use @ char anywhere. Is there any way to do this?