Skip to main content
abhayd3663
October 31, 2022
Question

MySQL stored procedure does not recognize parameter while executing.

  • October 31, 2022
  • 2 replies
  • 0 views

I have a simple stored proc as following.

DELIMITER $$
CREATE DEFINER=`dbadmin`@`%` PROCEDURE `Test_TruncateTable`(IN `spTableName` TEXT)
MODIFIES SQL DATA
COMMENT 'Truncates staging table.'
BEGIN

TRUNCATE TABLE spTableName;

END$$
DELIMITER ;

The procedure won't get executed, it throw's following error.

#1.

#2.

I tried changing the IN parameter to VARCHAR etc. but no luck. In past I have wrote more complicated procedures having multiple parameters however never run into the above mentioned error. 

2 replies

November 1, 2022

Hi, check the below code this error happens because the stored procedure is running the DDL statement.

DELIMITER $$
CREATE DEFINER=`dbadmin`@`%` PROCEDURE `Test_TruncateTable`(IN `spTableName` TEXT)
MODIFIES SQL DATA
COMMENT 'Truncates staging table.'
BEGIN

EXECUTE IMMEDIATE CONCAT('TRUNCATE TABLE ', spTableName, ';');

END$$
DELIMITER ;

abhayd3663
February 11, 2023

Thanks. The use of Execute() function did the job.