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.
Related
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.
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;
Please consider the following function defination. I created and set it up on MySQL 5.1 but it's failing in MariaDB 5.5
CREATE DEFINER=`root`#`127.0.0.1` FUNCTION `weighted_mean_by_kpi`(`KPIID` INT, `employee_id` INT, `date` DATE)
RETURNS decimal(6,3)
LANGUAGE SQL
DETERMINISTIC
READS SQL DATA
SQL SECURITY DEFINER
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE rating_number INT DEFAULT 0;
DECLARE rating_count INT DEFAULT 0;
DECLARE rating_total INT DEFAULT 0;
DECLARE weighted_total DOUBLE DEFAULT 0;
DECLARE cur CURSOR FOR
SELECT COUNT(rating), rating FROM employees_KPIs WHERE kpi_id = KPIID AND employee_id = employee_id AND employees_KPIs.created_at LIKE CONCAT("%",DATE_FORMAT(date,'%Y-%m'),"%") GROUP BY rating;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
RATING: LOOP
FETCH cur INTO rating_count, rating_number;
IF done = 1 THEN
LEAVE RATING;
END IF;
SET weighted_total = weighted_total + (rating_number * rating_count);
SET rating_total = rating_total + rating_count;
END LOOP RATING;
return (weighted_total/rating_total);
#return (weighted_total);
CLOSE cur;
END
I get the following error:
ERROR 1064 (42000) at line 1: 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 8
Thanks
Mysql sees the ';' delimiters in the function and breaks your CREATE FUNCTION statement.
To avoid this, change the delimiter before you define the function, and then change it back afterward:
Like:
DELIMITER //
-- your create function definition statement here
//
DELIMITER ;
As in your code the first ; semicolon was found at line 8, it tried to execute it the code up to the ';', and the syntax was invalid because it was incomplete (BEGIN without END).
I cannot copy stored procedure from MySQL 5.0.5 to MySQL 5.0.1.
Although I can copy all the tables. When I copy stored procedure it gives 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 'PROCEDURE `insertAccumCursor`()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLAR' at line 1
the stored procedure on 5.0.51b-community-nt is
CREATE DEFINER=`nobody`#`localhost` PROCEDURE `insertAccumCursor`()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE userid INT;
DECLARE counter INT;
DECLARE cur1 CURSOR FOR SELECT id FROM users WHERE InstitutionID='ReportTest';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
Set counter = 1;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO userid;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO myeltaccumgradebook VALUES (counter +12000,0, userid, 'ReportTest', 576389201, 0,1,1,1,1,CEIL(RAND() * 99) +50, '2013-07-10 16:10:54','2013-07-10 16:10:54', 0);
set counter = counter+1;
END LOOP;
CLOSE cur1;
END
How do you make procedure insert in new DB? With which tool? If you use phpmyadmin I can suggest you to save stored procedure to file and try to import it with cli
I am trying to write trigger in Mysql (5.1), but getting following error, please help.
The error is:
SQL Error (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.
Purpose for writing trigger:
I am writing application where I am assigning users, and I want to store unassigned usercount to field cluster_count in IX_branchdetails table.After updating the base table.
trigger:
DELIMITER $$
CREATE TRIGGER upd_trg AFTER
UPDATE ON DBNAME.BASETABLE
FOR EACH ROW
BEGIN
DECLARE m_branchcode INTEGER;
DECLARE cnt INTEGER;
DECLARE cursor_branch CURSOR FOR
SELECT DISTINCT branchcode
FROM ix_branchdetails;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cursor_branch;
my_loop: loop
set done = false;
fetch cursor_branch into m_branchcode;
if done then
leave my_loop;
end if;
select count(1) into cnt from (select count(1) from BASETABLE Where IX_BRANCHCODE = m_branchcode) as temp;
update DBANAME.ix_branchdetails set DBANAME.ix_branchdetails.cluster_count = cnt where DBANAME.ix_branchdetails.BRANCHCODE = m_branchcode;
end loop my_loop;
close cursor_branch;
END $$
DELIMITER ;
I don't see a declare for the done variable:
DECLARE done TINYINT DEFAULT FALSE;
The semicolon (;) is the default delimiter for MySQL statements. To get a procedure/function/trigger defined, we normally see the statement delimiter changed to a string that doesn't appear in the statement:
DELIMITER $$
CREATE PROCEDURE ...
END$$
DELIMITER ;
If the delimiter is not changed from the semicolon, then when MySQL encounters the first semicolon in your procedure/function/trigger, it sees that as the end of the statement, which is not what you want. You want MySQL to see the entire block of code as a single statement.