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;
Related
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.
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?
For some strange reason, inserting from stored procedure is not working.
This is what Im trying to do:
DROP TABLE IF EXISTS test;
CREATE TABLE IF NOT EXISTS test(
id INT(9) NOT NULL AUTO_INCREMENT
,name VARCHAR(30) NOT NULL
,PRIMARY KEY (id)
) DEFAULT CHARSET=utf8;
insert into test (name) values('A');
Inserting from command line works with no problems.
Then I created a stored procedure to do the same kind of insert:
DROP PROCEDURE IF EXISTS storedtest;
DELIMITER $$
CREATE PROCEDURE storedtest()
BEGIN
declare insert_sql varchar(200);
SET insert_sql = 'insert into test (name) values(3)';
SELECT insert_sql;
PREPARE mystm FROM #insert_sql;
EXECUTE mystm;
END$$
DELIMITER ;
call storedtest();
This gives me the error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NULL' at line 1
NULL? Where did NULL came from?
I also tried changing the sql-insert to look like this (dont know if it is a good way):
SET insert_sql = "insert into test (name) values('3')";
But mysql gives me exactly the same error.
Anyone has a clue?
The NULL MySQL is reporting is an empty user variable #insert_sql, which is different from the local stored procedure local variable insert_sql which you allocated with DECLARE.
MySQL's DECLARE is used for variables local to a stored program, but according to the documentation, PREPARE stmt FROM ... expects either a string literal or a user variable, which are the type preceded with #.
PREPARE stmt_name FROM preparable_stmt
preparable_stmt is either a string literal or a user variable that contains the text of the SQL statement.
You can allocate the untyped user variable with SET so there is no need for DECLARE. You may wish to set it to NULL when you're finished.
DROP PROCEDURE IF EXISTS storedtest;
DELIMITER $$
CREATE PROCEDURE storedtest()
BEGIN
-- Just SET the user variable
SET #insert_sql = 'insert into test (name) VALUES (3)';
SELECT #insert_sql;
-- Prepare & execute
PREPARE mystm FROM #insert_sql;
EXECUTE mystm;
-- Deallocate the statement and set the var to NULL
DEALLOCATE PREPARE mystm;
SET #insert_sql = NULL;
END$$
DELIMITER ;
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.