I have written following Mysql stored procedure with 2 input parameters (ids and tags)
SET #sql = Concat('UPDATE tbl_Members SET TagId=CONCAT(TagId,''',tags,''') WHERE MemberID IN (',ids,')');PREPARE stmt FROM #sql;EXECUTE Stmt;
When I try to execute that it throws the following error
The following query has failed: "CREATE DEFINER=`0zrt`#`localhost` PROCEDURE `update_member_tag`(IN `ids` VARCHAR(255), IN `tags` VARCHAR(255)) NOT DETERMINISTIC MODIFIES SQL DATA SQL SECURITY DEFINER SET #sql = CONCAT('UPDATE tbl_Members SET TagId=CONCAT(TagId,''',tags,''') WHERE MemberID IN (',ids,')'); PREPARE stmt FROM #sql; EXECUTE stmt;"
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 stmt FROM #sql; EXECUTE stmt' at line 2
Related
Query is retrieving correct result but in Prepared statement getting error "Error Code: 1064. check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO audittrail(BATCHID,IPADDRESS,DATE,LINENAME,ACTION) values (1165' at line 2"
Thanks in advance:-)
CREATE DEFINER = 'root'#'localhost'
PROCEDURE new_procedure()
BEGIN
DECLARE stmt text;
SET #stmt = (SELECT CONCAT('START TRANSACTION; \n', GROUP_CONCAT(objdata SEPARATOR ';\n'),';\nCOMMIT; \n DELETE FROM qry_obj_dtl where id in (',GROUP_CONCAT(ID SEPARATOR','),');') AS objData1 FROM qry_obj_dtl);
SELECT #stmt;
PREPARE param_stmt FROM #stmt;
EXECUTE param_stmt;
DEALLOCATE PREPARE param_stmt;
END
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 ;
I have the following stored procedure:
CREATE PROCEDURE getLastValueAutomaticShelter(IN fieldName varchar(30), position_number INT)
BEGIN
SET #query = CONCAT('SELECT * FROM automatic_changes WHERE',fieldName,'IS NOT NULL AND P_id=?');
PREPARE stmt FROM #query;
SET #position_number=position_number;
EXECUTE stmt USING #position_number;
DEALLOCATE PREPARE stmt;
END
Then I am running it:
mysql> call getLastValueAutomaticShelter('current_level', 500)//
And getting the following error:
ERROR 1064 (42000): 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 'NOT N
ULL AND P_id=?' at line 1
Any ideas? Thanks.
You need to add some spaces in there:
SET #query = CONCAT('SELECT * FROM automatic_changes WHERE ',fieldName,' IS NOT NULL AND P_id=?');
/* right ^ here....and ^ here*/
Otherwise your final query might look like this:
SELECT * FROM automatic_changes WHEREcolumnameIS NOT NULL AND P_id='whatever';
You get the idea :)
Quick question,
using the code bellow, im able to use a sp to perform a delete from any table, but i dont know how to use concat to delete with the where statement.
CREATE DEFINER = `root`#`%` PROCEDURE `fn_del_t`(in t_name varchar(50), isrv char(50))
BEGIN
set #table_name = t_name;
set #iserver = isrv;
# not working with where.
set #sql_text = concat('delete from ',#table_name, 'where iserver =',#iserver);
# ---- not working with where
prepare stm from #sql_text;
execute stm;
DEALLOCATE prepare stm;
END;
call fn_del_t('the_table','localhost');
The error im receiving is:
[SQL] call fn_del_t('the_table','localhost');
[Err] 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 'iserver =localhost' at line 1
Sorry for my english.
Able to solve with:
CREATE DEFINER = `root`#`%` PROCEDURE `fn_del_t`(in t_name varchar(50), isrv char(50))
BEGIN
set #table_name = t_name;
set #iserver = isrv;
#
set #sql_text = concat("delete from ",#table_name," where iserver ='",#iserver,"'");
prepare stm from #sql_text;
execute stm;
DEALLOCATE prepare stm;
END
This is the stored procedure I'm trying to use
DELIMITER ##
CREATE PROCEDURE exportFile()
BEGIN
DECLARE filename VARCHAR(255);
SET filename = CONCAT('~/Sample',NOW(),'.csv'));
SET #outfilestmt = concat('SELECT * INTO OUTFILE ',"'", filename,"'",' FROM Results') ;
PREPARE stmt FROM #outfilestmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END ##
DELIMITER ;
This is the error I get
ERROR 1064 (42000): 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 ');
SET #outfilestmt = concat('SELECT * INTO OUTFILE ',"'", filename,"'",' FROM R' at line 6
Desired Result:
call exportFile
--~/Sample2012-03-14-10:42:51.cvs
call exportFile
--~/Sample2012-03-14-10:42:52.cvs
call exportFile
--~/Sample2012-03-14-10:42:53.cvs
A semicolon is missing after
SET filename = CONCAT('~/Sample',NOW(),'.csv'))
and one brace is too much (also noted by Devart. Thanks). Change it to
SET filename = CONCAT('~/Sample',NOW(),'.csv');
Tested it to be sure. Works!