Mysql Syntax error when using prepared statement - mysql

I am creating a stored procedure which will be executing a prepared statement but on the creation the procedure I get mysql syntax error :
MySQL said: #1064 - You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'PREPARE udpatestatut FROM #req; EXECUTE
updatestatut; DEALLOCATE PREPARE upd' at line 4
I don't see what my mistake is so I need help to understand what I am doing wrong or writting wrong. Here down is the instructions I am executing.
CREATE PROCEDURE changestatut(IN pstatut VARCHAR(10), IN pidpost VARCHAR(255))
COMMENT 'This procedure change the statut of activities.' NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER
SET #req := concat('UPDATE compte set statut=\'', pstatut ,'\' where idpost in (', pidpost ,')');
PREPARE udpatestatut FROM #req; EXECUTE updatestatut; DEALLOCATE PREPARE updatestatut;
idpost parameter is a comma separated value ...e.g: 1,2,3,6

If you have multiple statements in your procedure you need to wrap them in a begin..end you might also be not setting delimiters. Try this
DROP PROCEDURE IF EXISTS changestatut;
DELIMITER $$
CREATE PROCEDURE changestatut(IN pstatut VARCHAR(10), IN pidpost VARCHAR(255))
COMMENT 'This procedure change the statut of activities.' NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER
BEGIN
SET #req := concat('UPDATE compte set statut=\'', pstatut ,'\' where idpost in (', pidpost ,')');
PREPARE udpatestatut FROM #req; EXECUTE updatestatut;
DEALLOCATE PREPARE updatestatut;
END $$
DELIMITER ;

Related

Error when executing a mysql store procedure

I have created a simple stored procedure that will drop a view. I am learning stored procedures in mysql so bear with me,
DELIMITER $$
CREATE PROCEDURE dropView (
IN viewName varchar(4000)
)
BEGIN
SET #sql:=CONCAT('DROP VIEW ',#viewName);
PREPARE dynamic_statement FROM #SQL;
EXECUTE dynamic_statement;
DEALLOCATE PREPARE dynamic_statement;
END$$
DELIMITER ;
And i am calling the above like so,
SET #theView = '`Report`;';
CALL dropView(#theView);
I am getting the error message,
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1
I am using mysql 8.0.17
According to this i can use 'CREATE/DROP VIEW' in a prepared mysql.
#viewNameis a 9.4. User-Defined Variables and viewName one stored procedure parameter (13.1.17 CREATE PROCEDURE and CREATE FUNCTION Statements), are different variables.

"for the right syntax to use near '0' at line 1" in Stored Procedure

Very weird bug taking place. I have a stored procedure that works fine on the Live Server (MySQL v. 5.5.12) but on local (v. 5.5.25a) I get an error. Here's the Stored Procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS `SaveIDForTable`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `SaveIDForTable`(IN OffID varchar(100),IN TabName VARCHAR(255))
DETERMINISTIC
BEGIN
set #sql='create table if not exists '||TabName||'(ID bigint(20) NOT NULL,exp date not null,PRIMARY KEY (ID))';
PREPARE statment FROM #sql;
EXECUTE statment;
DEALLOCATE PREPARE statment;
set #sql='insert ignore into '||TabName||' select ID,curdate() from all_data where Official_ID=\''||OffID||'\'';
PREPARE statment FROM #sql;
EXECUTE statment;
DEALLOCATE PREPARE statment;
END$$
But When I run the Stored Procedure
CALL SaveIDForTable ('203200702000000-207-5C2FF0','testTable')
I get the error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0' at line 1
I've tried running other stored procs on my local machine and they work fine, but this one for some reason I can't get to work. Any help would be greatly appreciated.

EXECUTE in MySQL Transaction

I'm wrestling with MySQL stored procedures, and the PREPARE / EXECUTE statement pair. I'm attempting to run the (simplified) code below in order to create a stored procedure that will encapsulate several queries into a transaction with rollback. I continue to get
Error Code: 1064
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax
to use near 'goodID;
If I remove the transaction and handlers all is well with the code. If I remove the EXECUTEstatement the procedure can be created.
What am I missing here?
DELIMITER $$
USE `casc`$$
DROP PROCEDURE IF EXISTS `sp_T_MergeMemberIDs`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_T_MergeMemberIDs`(IN goodID VARCHAR(8), OUT param_sp_success TINYINT)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
DECLARE EXIT HANDLER FOR SQLWARNING ROLLBACK;
START TRANSACTION;
SET param_sp_success = 0;
SET #SQL=
"SELECT * FROM member
WHERE memberID = ?";
PREPARE stmt FROM #SQL;
EXECUTE stmt USING goodID;
-- queries executed here using the same parameter
-- omitted for simplicity
SET param_sp_success = 1;
COMMIT;
END$$
As far as I remember, you need to use session user variable in execute ...using :
...
PREPARE stmt FROM #SQL;
SET #tmp_var = goodID;
EXECUTE stmt USING #tmp_var;
...

Mysql stored procedure

What I want to do is,create a table in mysql by passing the table name as a parameter in the stored procedure.I'm using following code for stored procedure in mysql.
DELIMITER //
CREATE PROCEDURE createtable(IN tablename varchar(20))
BEGIN
SET #s=CONCAT('CREATE TABLE', tablename, '(month varchar(20))');
PREPARE stmt FROM #s;
EXECUTE stmt;
END //
and when i call it
CALL createtable('account');
I get the following error
You have an error in your SQL syntax; check the MySQL server version for the right syntax to us...
I don't know where I'm wrong..
You forgot the spaces before and after your table name. Try
DELIMITER //
CREATE PROCEDURE createtable(IN tablename varchar(20))
BEGIN
SET #s=CONCAT('CREATE TABLE ', tablename, ' (month varchar(20))');
PREPARE stmt FROM #s;
EXECUTE stmt;
END //

calling DROP USER from a stored proc, how to expand the input argument

I'm trying to set up a stored proc that (in part) drops a user. I've tried several versions of DROP USER that "expand" the argument and I can't find one that works. Can someone help?
DELIMITER //
CREATE PROCEDURE `dropuser`( IN inUser varchar(255) )
BEGIN
#DROP USER concat(inUser,"#localhost");
#DROP USER inUser "#localhost";
#DROP USER "inUser#localhost";
END//
DELIMITER ;
CALL dropuser("asdf");
All three of them fail in the same way:
ERROR 1064 (42000) at line 4: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(inUser,"#localhost");
Obviously I'm getting the expansion wrong.
delimiter //
drop procedure if exists dropuser //
create procedure dropuser( in inUser varchar(255) )
begin
set #str = concat('drop user ', "'",inUser,"'#","'localhost'");
prepare stmt from #str;
-- select #str;
execute stmt;
deallocate prepare stmt;
end//
delimiter ;
call dropuser('pippo');