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.
Related
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 two databases Local and Development.
I want to move Local Db's procedures to development Db but not all. I only want newly created procedures.
For that I am writing code. But I stuck. I have definition but i don't know how to execute it on another Database using SQL Editor(Workbench/Heidi SQL).
Consider below example:
SET #Proc_new_procedure = '
DELIMITER $$
CREATE PROCEDURE `new_procedure` (input1 varchar(50),input2 varchar(50))
BEGIN
SET input1 = LTRIM(input1);
select * from table1 where column1 = input1;
select * from table2 where column1 = input2;
END$$
DELIMITER ;
';
PREPARE stmt1 FROM #Proc_new_procedure;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
I got below error:
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 DELIMITER $$ CREATE PROCEDURE new_procedure (input1 varchar(50),input2 varcha' at line 1
I tried Prepare statement to create procedure. But Prepare statement allow me only one statement execution at a time.
Can somebody please help me?
I'm creating a sql script for a migration functionality of ours. We want to migrate data from one magento-instance to another (using pure SQL because the import/export of magento is pretty limited).
One of the challenges ist that I want to dynamically alter the AUTO_INCREMENT value of a table so it doesn't need to be done manually in multiple steps. I want to set the AUTO_INCREMENT value to the current-maximum value of the corresponding column + 1.
I prepared the following stored procedure for this:
DELIMITER $$
CREATE PROCEDURE alter_auto_inc_customer()
BEGIN
SELECT #max := MAX(entity_id)+ 1 FROM customer_entity;
PREPARE stmt FROM 'ALTER TABLE customer_entity AUTO_INCREMENT = ?';
EXECUTE stmt USING #max;
END $$
This command runs smoothly. Afterwards the procedure should just be called by a simple statement:
CALL alter_auto_inc_customer();
When I execute the "call"-statement, I get a 1064 Syntax error. It's probably trivial but I can't figure it out for the life of me...
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 '?' at line 1
Does anyone have an idea what the issue is?
I need to get this into one or more stored procedures because I need to be able to run similar statements for multiple tables in the database.
Instead of altering the table structure you can use a trigger to get the max value + 1 before insert data:
DELIMITER $$
DROP TRIGGER IF EXISTS custom_autoincrement_bi$$
CREATE TRIGGER custom_autoincrement_bi BEFORE INSERT ON customer_entity
FOR each ROW
BEGIN
SET NEW.entity_id = select max(entity_id) + 1 from customer_entity;
END$$
DELIMITER ;
But if you want to alter the table from stored procedure
DELIMITER $$
CREATE PROCEDURE alter_auto_inc_customer()
BEGIN
SELECT MAX(entity_id) + 1 into #max FROM customer_entity;
set #sql = concat('ALTER TABLE customer_entity AUTO_INCREMENT = ', #max);
PREPARE stmt FROM #sql;
EXECUTE stmt ;
DEALLOCATE PREPARE stmt;
END $$
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.
I am executing below procedure in Mysql.
DROP procedure IF EXISTS `wm_batch_list`;
DELIMITER $$
/*!50003 SET #TEMP_SQL_MODE=##SQL_MODE, SQL_MODE='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ $$
CREATE DEFINER=`root`#`localhost` procedure `wm_batch_list`(IN p_start int(11),IN p_range int(11))
BEGIN
select * from batch ORDER BY start_year DESC limit p_start,p_range;
END $$
/*!50003 SET SESSION SQL_MODE=#TEMP_SQL_MODE */ $$
DELIMITER ;
but i am getting this error:
Error code 1064, SQL state 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 'p_start,p_range;
END' at line 3
Please suggest where i am doing mistake?
Googling a bit i found a question related to yours at passing LIMIT as parameters to MySQL sproc
From http://dev.mysql.com/doc/refman/5.1/en/select.html:
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).
So the way to solve it is using a prepare statement
SET #skip=p_start;
SET #rows=p_range;
PREPARE STMT FROM 'SELECT * FROM table LIMIT ?, ?';
EXECUTE STMT USING #skip, #rows;