2

I'm new to MySQL, I'm trying to store sql statement into variable:

SET @sql = 'SELECT * FROM tbl_tableName';
  SELECT @sql;

It executes correctly, and give me result:

enter image description here

But how can see the result of the sql statement stored in that variable? If I'm execute it, it gives an error.

SET @sql = 'SELECT * FROM tbl_defects';
  SELECT @sql;
  PREPARE stmt FROM @sql;
  EXECUTE stmt;

error:

Fatal error: Uncaught Error: Call to a member function getClauses() on null in /opt/lampp/phpmyadmin/vendor/phpmyadmin/sql-parser/src/Utils/Query.php:567 Stack trace: #0

Please any one help me to execute the sql statement stored in variable (Want to execute/check the result stored in that variable. I know we can directly run and check the result in SQL prompt)?

1 Answer 1

1

To get a result for that variable you have to first create a stored procedure and execute that procedure you will get the answer.

Create stored procedure:

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `test`()
BEGIN  
SET @sql = ('SELECT * FROM tbl_defects');
  SELECT @sql;
  PREPARE stmt FROM @sql;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;

Hope this help you.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there any way to execute it without stored procedure ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.