Declare is not valid at this position, expecting - mysql

Have Declares which are giving unbefore seen errors, that have attempted to find the solution of. As well as weird as have previous procedures which use same syntax but do not throw any errors
This is for a procedure which uses cursors to retrieve information from a table, then insert it into another table, which is done so that the information within may be used to be able calculate a total, which is passed along along with some required values
BEGIN
DECLARE fin INT DEFAULT FALSE;
DECLARE pol VARCHAR(30) default '';
DECLARE total_accrued decimal(20,2) DEFAULT '0.00';
DECLARE total_paid decimal(20,2) DEFAULT '0';
DECLARE TOTAL DECIMAL(20,2) DEFAULT '0.00';
DECLARE user_pol varchar(45) default '';
DECLARE c3 CURSOr FOR SELECT total_accrued FROM fn_policy_mast;
DECLARE c4 CURSOR FOr SELECT total_amount_paid FROM fn_policy_mast;
DECLARE c2 CURSOR FOR SELECt policy_number FROM fn_policy_mast;
DECLARE c1 CURSOr FOR SELECT balance FROM fn_policy_mast;
-- DECLARE d_policy CURSOR FOR SELECT DISTINCT cod_policy FROM fn_policy_mast;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET fin = TRUE;
OPEN c1;
OPEN c2;
OPEN c3;
OPEN c4;
-- OPEN d_policy;
read_loop: LOOP
FETCH c3 INTO total_accrued;
FETCH c4 INTO total_paid;
FETCH c2 INTO pol;
set total = 0;
-- FETCH d_policy INTO user_pol;
-- IF user_pol = pol then
set total_accrued = (SELECT SUM(monthly_premium) FROM col_trans_log WHERE policy_number LIKE pol);
set total_paid = (SELECT SUM(transaction_amount) FROM col_Trans_log WHERE policy_number LIKE pol);
set total = total_paid - total_accrued;
-- END IF;
-- set TOTAL = (total_accrued-total_paid);
UPDATE fn_policy_mast set balance = TOTAL WHERE policy_number like pol;
IF fin=TRUE THEN
LEAVE read_loop;
END IF;
-- INSERT STATEMENTS HERE
-- set total = 0;
END LOOP;
select * FROM fn_policy_mast;
close c1;
close c2;
CLOSE c3;
CLOSE c4;
END//
DELIMITER ;````
Expected result to run, accepting the declares, actual experience is thaere is some form of syntax wrong, as it does not properly recognise the declare statements
This is 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 '' at line 5

DECLARE total_paid decimal(20,2) DEFAULT '0'
Is this the line?
Have you tried changing the default to '0.00' ?

Sorry guys, I made a dumb mistake and forget to include the delimiter before the procedure, afterwards it works.
Simply include a "Delimiter //" before the procedure

However, a stored procedure consists of multiple statements separated by a semicolon (;).
If you use a MySQL client program to define a stored procedure that contains semicolon characters, the MySQL client program will not treat the whole stored procedure as a single statement, but many statements.
Therefore, you must redefine the delimiter temporarily so that you can pass the whole stored procedure to the server as a single statement.
DELIMITER $$
CREATE PROCEDURE sp_name()
BEGIN[enter link description here][1]
-- statements
END $$
DELIMITER ;
Please refer attached link for more detail.

Related

Mysql - replace all col values by function

I need to modify all values in a column with a function.
I have wrote a function and added it to stored function.
I have wrote a stored procedure that will loop over all the values in the column with a cursor and switch the values.
This is a windows server running wamp, php 5.6, mysql 5.0
Tried ec2 linux server with mysql 5.6 as well.
DELIMITER $$
DROP PROCEDURE IF EXISTS `my_proc` $$
CREATE PROCEDURE `my_proc`()
BEGIN
DECLARE val1 INT DEFAULT NULL;
DECLARE val2 INT DEFAULT NULL;
DECLARE done TINYINT DEFAULT FALSE;
DECLARE cursor1 CURSOR FOR SELECT `col1` FROM `table1` WHERE 1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cursor1;
my_loop:
LOOP
FETCH NEXT FROM cursor1 INTO val1;
IF done = TRUE THEN
LEAVE my_loop;
ELSE
SET val2 = CALL the_other_procedure1(val1);
UPDATE `table1`
SET `col1` = val2
WHERE `col1` = val1 ;
END IF;
END LOOP my_loop;
END $$
DELIMITER ;
I get 2 errors when running it in sql section of phpmyadmin:
1 errors were found during analysis.
Unexpected character. (near ":" at position 364)
and:
#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 'CALL the_other_procedure1(val1);
UPDATE `table1`
SET `col1` = val2
WHER' at line 25
Here is an example on update in the same table we read from.
Two remarks:
The statement FETCH NEXT FROM cursor1 INTO val1; should become just FETCH cursor1 INTO val1;;
and
The assignment SET val2 = CALL the_other_procedure1(val1); should be called without keyword CALL, i.e SET val2 = the_other_procedure1(val1);.
Also make sure you the_other_procedure1() is a function (i.e. it must return some value).
Read more about differences between stored procedures and functions.

Handling unfound data in mySQL procedure loop

I think I'm narrowing in on my issue. I have a loop that is only firing once:
DELIMITER $$
DROP PROCEDURE IF EXISTS `thread_updates` $$
CREATE PROCEDURE `thread_updates`()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE my_curr_id INT DEFAULT NULL;
-- DECLARE other vars
DECLARE fixer_cursor CURSOR FOR
SELECT DISTINCT(id)
FROM log
WHERE date >= '2018-01-01';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN fixer_cursor;
REPEAT
FETCH fixer_cursor INTO my_curr_id;
IF NOT done THEN
SELECT data FROM table WHERE id = my_curr_id; -- This may not exist
-- Do other stuff with 'data' or NULL from above
END IF;
UNTIL done END REPEAT;
CLOSE fixer_cursor;
END $$
DELIMITER ;
I think the issue may be that inside the IF NOT done THEN loop, I have a few select statements that may be trying to select results that don't exist (not found).
This is fine (for me) as the logic continues along using NULL values in those spots, but I suspect that my CONTINUE HANDLER FOR NOT FOUND is catching the NOT FOUND warning that these selects throw inside the loop and are thus stopping the entire loop prematurely.
How can I listen for NOT FOUND warning on my cursor only?
Or, how can I suppress the NOT FOUND warning in my MAYBE FOUND select statements inside my loop so my loop continues?
I think I have solved the issue by implementing a counter in a loop rather than relying on the NOT FOUND handler:
DELIMITER $$
DROP PROCEDURE IF EXISTS `thread_updates` $$
CREATE PROCEDURE `thread_updates`()
BEGIN
DECLARE my_total INT DEFAULT NULL; -- Declare total
DECLARE my_counter INT DEFAULT 0; -- Declare counter starting at 0
DECLARE my_curr_id INT DEFAULT NULL;
-- DECLARE other vars
DECLARE fixer_cursor CURSOR FOR
SELECT DISTINCT(id)
FROM log
WHERE date >= '2018-01-01';
OPEN fixer_cursor;
SELECT FOUND_ROWS() INTO my_total; -- Get total number of rows
my_fixerloop: LOOP
FETCH fixer_cursor INTO my_curr_id;
IF my_counter >= my_total THEN -- Compare counter to total
CLOSE fixer_cursor;
LEAVE my_fixerloop;
END IF;
SET my_counter = my_counter + 1; -- Increment by one for each record
SELECT data FROM table WHERE id = my_curr_id; -- This may not exist
-- Do other stuff with 'data' or NULL from above
END LOOP;
END $$
DELIMITER ;

Mysterious error in CREATE PROCEDURE in MariaDB/MySQL

I tried to make a simple procedure in MariaDB 10.2 but I encountered an issue regarding variables defining.
I am receiving (conn:107) 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 '' at line 3 message when I declare a variable.
I read the MariaDB documentation and I it says that a variable is defined like this DECLARE var_name [, var_name] ... type [DEFAULT value]
Where I am wrong? I am coming from Oracle SQL and some sintax is wired for me.
I use Eclipse with MariaDB JDBC to connect on SQL.
CREATE PROCEDURE nom_jobs_insert(IN p_name varchar(100) CHARACTER SET 'utf8')
BEGIN
DECLARE counter INT DEFAULT 0;
SELECT count(*) INTO counter
FROM nom_jobs
WHERE lower(name) = lower(p_name)
IF counter = 1 THEN
INSERT INTO nom_jobs(name) VALUES (p_name);
END IF;
END;
I found the solution.
In MariaDB you have to define a delimiter before create a procedure and you need to mark where the procedure code is finished.
DELIMITER //
CREATE PROCEDURE nom_jobs_insert(IN p_name varchar(100) CHARACTER SET 'utf8')
BEGIN
DECLARE counter INT DEFAULT 0;
SELECT count(*) INTO counter
FROM nom_jobs
WHERE lower(name) = lower(p_name);
IF counter = 1 THEN
INSERT INTO nom_jobs(name) VALUES (p_name);
END IF;
END; //
You have error not in DECLARE expression, add ; after SELECT statement
Here are the clues that point to a missing DELIMITER:
near '' at line 3
Line 3 contains the first ;
When the error says near '', the parser thinks it has run off the end of the "statement".
Put those together -- it thinks that there is one 3-line statement ending with ;. But the CREATE PROCEDURE should be longer than that.
CREATE PROCEDURE nom_jobs_insert(IN p_name varchar(100) CHARACTER SET 'utf8')
IS
DECLARE counter INTEGER DEFAULT 0;
BEGIN
SELECT count(*) INTO counter
FROM nom_jobs
WHERE lower(name) = lower(p_name)
IF counter = 1 THEN
INSERT INTO nom_jobs(name) VALUES (p_name);
END IF;
END;

Getting Syntax Error in MySQL Stored Procedure while trying nested Cursors

I have a MySQL Stored Procedure Script in which I am trying to implement a nested cursors . Following is my code
DELIMITER //
DROP PROCEDURE IF EXISTS PopulateStaleEntityTable//
DELETE FROM bucket_stale;
LOAD DATA LOCAL INFILE '/home/centos/mysql1.txt' INTO TABLE bucket_stale;
CREATE PROCEDURE PopulateStaleEntityTable()
BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE finished_stale INTEGER DEFAULT 0;
DECLARE product_offer_id_var bigint(20) DEFAULT 0;
DECLARE product_offer_group_id_var bigint(20) DEFAULT 0;
DECLARE batch_count INTEGER DEFAULT 0;
DECLARE max_batch_count INTEGER DEFAULT 100;
DECLARE bucket_id_var bigint(10) DEFAULT 0;
DECLARE stale_cursor CURSOR FOR
SELECT staleid FROM bucket_stale;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished_stale = 1;
OPEN stale_cursor;
insert_stale : LOOP
FETCH stale_cursor INTO bucket_id_var;
SELECT bucket_id_var;
BEGIN
DECLARE product_cursor CURSOR FOR
SELECT product_offer_id FROM product where bucket_id='1086';
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
OPEN product_cursor;
insert_entity: LOOP
START TRANSACTION;
FETCH product_cursor INTO product_offer_id_var;
SELECT product_offer_group_id INTO product_offer_group_id_var FROM product_offer where id=product_offer_id_var;
INSERT IGNORE INTO stale_cached_entity (entity_type,entity_id,cache_action,created,field_change) VALUES ('PRODUCT_OFFER_GROUP',product_offer_group_id_var, 'UPDATE',now(),'DEFAULT_SUPC_LIST1');
SET batch_count=batch_count+1;
IF batch_count > max_batch_count THEN
COMMIT;
SET batch_count=0;
END IF;
IF finished = 1 THEN
LEAVE insert_entity;
END IF;
END LOOP insert_entity
END;
IF finished_stale = 1 THEN
LEAVE insert_stale;
END IF;
END LOOP insert_stale;
CLOSE stale_cursor;
END//
DELIMITER ;
When i try and run this script it gives the following 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 'END;
IF finished_stale = 1 THEN
LEAVE insert_stale;
END IF; ' at line 38
I tried to figure out whats wrong but i couldn't . What am I missing ?
The problem lies in this line:
END LOOP insert_entity
If you review the example of cursor code at https://dev.mysql.com/doc/refman/5.7/en/cursors.html you see that END LOOP does not need the label, and it needs a semicolon statement terminator.
Like this:
END LOOP;
I agree with #tadman's comment. I understand some developers who have had experience with Oracle or Microsoft have an expectation of using stored procedures, but frankly, MySQL's stored procedure support sucks so bad you will be happier and more productive writing some simple Python scripts (or any other language).

mysql stored proc is not working from mysqlworkbench or java code

I have written a stored proc in mysql when i am running it through hopper it is working fine but when i am trying to run it from mysql workbench or java it is not returning any result and also not showing any exception
I will request you to please help me on this
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `issueitem`()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE inventoryids INT DEFAULT 0;
DECLARE batch varchar(39);
DECLARE resultstr varchar(3000) DEFAULT '';
DECLARE exp DATE;
DECLARE mfgdate DATE;
DECLARE availableunit INT;
DECLARE quantity INT DEFAULT 100;
DECLARE oldest_date DATETIME;
DECLARE cur_count INT;
DECLARE que_size INT DEFAULT 0;
DECLARE curs CURSOR FOR SELECT inventoryid,batch,exp,availableunit FROM aashramdata.inventory where itemid=1 ORDER BY exp ASC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN curs;
SET cur_count=quantity;
read_loop: LOOP
FETCH curs INTO inventoryids,batch,exp,availableunit;
IF done THEN
LEAVE read_loop;
END IF;
SET que_size = que_size + availableunit;
IF cur_count >= availableunit THEN
set cur_count=cur_count-availableunit;
set resultstr=CONCAT(resultstr,batch,' - ',exp,' - ',availableunit,' - ');
update aashramdata.inventory set `availableunit`=0 where inventoryid=inventoryids;
END IF;
IF cur_count < availableunit THEN
update aashramdata.inventory set `availableunit`=availableunit-cur_count where inventoryid=inventoryids;
set resultstr=CONCAT(resultstr,batch,' - ',exp,' - ',availableunit-cur_count,' - ');
set cur_count=0;
END IF;
IF que_size >= quantity then
LEAVE read_loop;
END IF;
END LOOP;
CLOSE curs;
select resultstr;
END
Since the procedure is apparently syntactically valid, it's impossible to answer what the problem might be, without some knowledge of the data.
The easiest way to debug a stored procedure is by peppering it with unbounded SELECT statements (that is, selects that are not part of a subquery and not part of an INSERT ... SELECT or SELECT ... INTO) and then running it from the MySQL command line client, which handles multiple result sets from stored procedures much more gracefully than most graphical clients.
For example:
...
SET cur_count=quantity;
SELECT cur_count; -- add this
read_loop: LOOP
FETCH curs INTO inventoryids,batch,exp,availableunit;
SELECT inventoryids,batch,exp,availableunit; -- add this
SELECT done; -- add this
IF done THEN
...
When run from the command line client...
mysql> CALL issueitem();
...output will start rolling out of the console at you, showing you the internal values the procedure is encountering as it iterates the loop.
This, or other SELECT statements like this added elsewhere, will expose the internal variables and this should help you find the problem. You'll need to remove them before you call the procedure from code or even the GUI since the GUI may not handle them well -- it may start opening new tabs or splitting panes or just ignoring everything after the first SELECT.
Note that the last iteration of the loop, you may see the values from the previous iteration repeated since the failed read from the cursor may not reset the variables, but "done" will also transition from 0 to 1 indicating that the cursor has run out of rows and fired the CONTINUE HANDLER, so those values won't actually have been processed twice.