I got a hint from O.jones and went to create a stored function.
That's what I created:
DELIMITER $$
CREATE FUNCTION change_int(colres VARCHAR(500)) RETURNS INT(11)
BEGIN
DECLARE res int(11);
DECLARE leng int(11);
DECLARE newres int(11);
DECLARE mult int(11);
DECLARE temp1 int(11);
DECLARE temp2 int(11);
SET res = CAST(colres AS UNSIGNED);
SET leng = CHAR_LENGTH(CAST( colres AS CHAR));
SET newres = 0;
SET mult = 1;
SET temp1 = 0;
SET temp2 = 0;
WHILE (res > 0) DO
SET temp1 = MOD(res , 10 );
SET res = (res DIV 10);
SET temp2 = MOD(res , 10 );
SET newres = (newres +((temp1 + temp2 ) * mult));
SET mult = mult*10;
END WHILE;
SET newres = SUBSTRING (newres, 1, leng );
RETURN newres;
END $$
DELIMITER ;
But I get error when I ty to run it on line 6 :
#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 ''res' = CAST(colres AS CHAR)' at line 6
Added a delimiter, new 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 'SET newres = SUBSTRING (newres, 1, leng );
RETURN newres;
END' at line 27
Greate now it worked.
But I can not evoke her:
Ran a test with:
SHOW FUNCTION STATUS;
And it exists.
When I try to ran it like this:
UPDATE `table` SET `col1` = function_name(`col1`);
Also tried:
UPDATE `table` SET `col1` = db.function_name(`col1`);
No luck.
You have missing column before END WHILE. It must be END WHILE;
Working example
Related
I try to update some columns using trigger before insert
DROP TRIGGER IF EXISTS update_p_posts_places;
DELIMITER $$
CREATE TRIGGER update_p_posts_places BEFORE
INSERT
ON
`p_posts` FOR EACH ROW
BEGIN
DECLARE
p_post_group_id_ int;
SELECT
`p_post_subgroup`.`p_post_group_id`
INTO
p_post_group_id_
FROM
`p_post_subgroup`
WHERE
`p_post_subgroup`.`p_post_subgroup_id` = NEW.p_post_subgroup_id;
IF(p_post_group_id_ = 5) THEN
BEGIN
DECLARE
place1_id_ int;
place2_id_ int;
place3_id_ int;
place4_id_ int;
place5_id_ int;
SELECT
`Places`.`place1_id`,
`Places`.`place2_id`,
`Places`.`place3_id`,
`Places`.`place4_id`,
`Places`.`place5_id`
INTO
place1_id_, place2_id_, place3_id_, place4_id_, place5_id_
FROM
`Places`
WHERE
`Places`.`place5_id` = NEW.p_post_place_id LIMIT 1;
SET NEW.place5_id = place5_id_;
SET NEW.place1_id = place1_id_;
SET NEW.place2_id = place2_id_;
SET NEW.place3_id = place3_id_;
SET NEW.place4_id = place4_id_;
END $$
ELSE
SET NEW.place5_id = NULL;
END IF;
END $$
DELIMITER ;
It's showing some syntax errors.
#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 'DECLARE
place1_id_ int;
place2_id_ int;
' at line 20
Here is a working trigger. I tested creating it on MySQL 5.7.
CREATE TRIGGER update_p_posts_places BEFORE INSERT ON `p_posts`
FOR EACH ROW
BEGIN
-- all declarations must be before any other statements
DECLARE p_post_group_id_, place1_id_, place2_id_, place3_id_,
place4_id_, place5_id_ int;
SELECT
`p_post_subgroup`.`p_post_group_id`
INTO
p_post_group_id_
FROM
`p_post_subgroup`
WHERE
`p_post_subgroup`.`p_post_subgroup_id` = NEW.p_post_subgroup_id;
IF(p_post_group_id_ = 5) THEN
SELECT
`Places`.`place1_id`,
`Places`.`place2_id`,
`Places`.`place3_id`,
`Places`.`place4_id`,
`Places`.`place5_id`
INTO
place1_id_, place2_id_, place3_id_, place4_id_, place5_id_
FROM
`Places`
WHERE
`Places`.`place5_id` = NEW.p_post_place_id LIMIT 1;
SET NEW.place5_id = place5_id_;
SET NEW.place1_id = place1_id_;
SET NEW.place2_id = place2_id_;
SET NEW.place3_id = place3_id_;
SET NEW.place4_id = place4_id_;
ELSE
SET NEW.place5_id = NULL;
END IF;
END $$
You can use one DECLARE for multiple local variables, but you must do like var1, var2, var3, ... int. In other words, name the type only once at the end. See documentation: https://dev.mysql.com/doc/refman/8.0/en/declare-local-variable.html
No need for the BEGIN..END inside the IF and definitely do not use $$ until after the last END because that will terminate the parser's interpretation of your whole CREATE TRIGGER statement before it's complete.
What is the correct way to generate a new id as varchar? Here I'm trying to convert from SQL Server to MySQL database.
Help needed.
I tried internet but it did not solve my problem.
--*GENERATES ANY ID WHEN NEED*
DELIMITER //
CREATE FUNCTION getNewID(needTable VARCHAR(20)) RETURNS VARCHAR(10)
BEGIN
DECLARE lastvalue VARCHAR(10);
DECLARE i INT;
DECLARE newId VARCHAR(10);
IF needTable = 'Item'
SELECT lastvalue = MAX(resourceID) FROM Item;
SELECT MAX(resourceID) INTO lastvalue FROM Item;
IF IS NULL(lastvalue)
SET lastvalue = 'I00000000';
SET i = RIGHT(lastvalue,9) + 1;
SET newId = 'S' + RIGHT('00000000'+CONVERT(VARCHAR(10),i),9);
RETURN newId;
END; //
DELIMITER ;
SELECT getNewID ('Item');
DROP FUNCTION getNewID
The error says:
Error code 1064, SQL state 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 'SELECT MAX(resourceID) INTO lastvalue FROM Item;
IF IS NULL(lastval' at line 10
Line 3, column 1
Execution finished after 0 s, 1 error(s) occurred.
Try this
DELIMITER //
CREATE FUNCTION getNewID(needTable VARCHAR(20)) RETURNS VARCHAR(10)
BEGIN
DECLARE lastvalue, newId VARCHAR(10);
DECLARE i INT;
SELECT MAX(resourceID) INTO lastvalue FROM Item where needTable = 'Item';
IF(lastvalue IS NULL) THEN
SET lastvalue = 'I00000000';
SET i = RIGHT(lastvalue,9) + 1;
SET newId = 'S' + RIGHT('00000000'+CONVERT(VARCHAR(10),i),9); -- what you are trying to do here?
END IF;
-- what you need to return whenlastvalue is not null?
RETURN newId;
END //
DELIMITER ;
I am trying to:
1. Print all the values in between #loop_start_date to #loop_end_date
2. loop_start_date should be increased by 1 here till loop_start_date=loop_end_date
I tried below but Mysql says
#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 'BEGIN Declare #user_id int' at line 3
Here is the procedure code
CREATE PROCEDURE generatePayscale
BEGIN
Declare #user_id int;
Declare #office_id int;
Declare #loop_start_date Date;
Declare #loop_end_date Date;
SET #user_id = 1287;
SET #office_id = 8;
SET #loop_start_date = '2018-06-02';
SET #loop_end_date = '2018-06-06';
WHILE(#loop_start_date < #loop_end_date) do
SELECT old_paysetupid
FROM personneltransfer personnelid=#user_id
AND pt.transferdate>#loop_start_date
END WHILE;
END
This may point you in the correct direction, a few things i would change. Added BEGIN. Then added declare for each of the vars. Then you need a where after the from
SET #user_id = 1287;
SET #office_id = 8;
SET #loop_start_date = '2018-06-02';
SET #loop_end_date = '2018-06-06';
WHILE(#loop_start_date < #loop_end_date) do
BEGIN
SELECT old_paysetupid
FROM personneltransfer where personnelid=#user_id
and pt.transferdate>#loop_start_date
END WHILE;
My procedure gives error when I run. I couldn't find where is mistake ?
CREATE PROCEDURE testing ()
BEGIN
DECLARE i INT;
DECLARE vSite VARCHAR(100);
set #i = 1;
BEGIN WHILE #i <= 5 DO
SET #vSite = #vSite + CONCAT('LINE '+#i+', ');
SET #i = #i + 1;
END WHILE
SELECT #vSite;
END
/* Error
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 'SELECT #vSite; END' at line 10
*/
/* required output given below
LINE 1, LINE 2, LINE 3, LINE 4, LINE 5,
*/
Resolved By My Self
delimiter //
CREATE procedure while_examples()
wholeblock:BEGIN
declare str VARCHAR(255) default '';
declare x INT default 0;
SET x = 1;
WHILE x <= 5 DO
SET str = CONCAT(str,'LINE',x,',');
SET x = x + 1;
END WHILE;
select str;
END//
CALL while_examples();
I got this error message from my MySQL code. But I don't know where is my fault..
Here's my code:
DELIMITER $$
USE `kp`$$
DROP PROCEDURE IF EXISTS `getAllUmurPegawai`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getAllUmurPegawai`()
BEGIN
DECLARE currdate INT;
DECLARE birthdate INT;
DECLARE numRows INT;
DECLARE numIteration INT;
DECLARE tempMonth INT;
SET numRows = SELECT COUNT(*) FROM pegawai;
SET numIteration = 1;
WHILE numIteration <= numRows DO
SET currdate = (SELECT EXTRACT(YEAR FROM CURRENT_DATE));
SET birthdate = (SELECT EXTRACT(YEAR FROM (SELECT TGL_LAHIR FROM pegawai WHERE INDEXING = numIteration)));
SET umur = currdate - birthdate;
SET tempMonth = SELECT EXTRACT(MONTH FROM (SELECT TGL_LAHIR FROM pegawai WHERE INDEXING = numIteration));
IF umur < 56 THEN
INSERT INTO pegawai(STATUS_PEGAWAI,BULAN_PENSIUN(JIKA_SUDAH)) VALUES('Belum Pensiun',0);
ELSE
INSERT INTO pegawai(STATUS_PEGAWAI,BULAN_PENSIUN(JIKA_SUDAH)) VALUES('Pensiun',tempMonth);
END IF;
SET numIteration = numIteration + 1;
END WHILE;
END$$
DELIMITER ;
and i get this error message:
Query : CREATE DEFINER=`root`#`localhost` PROCEDURE `getAllUmurPegawai`() BEGIN DECLARE currdate INT; DECLARE birthdate INT; DECLARE num...
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 'SELECT COUNT(*) FROM pegawai;
SET numIteration = 1;
WHILE numIteration <= numR' at line 8
How can i solve this problem? Best Answer will be appreciated :))
To use a SELECT query as an expression you have to put it in parentheses:
SET numRows = (SELECT COUNT(*) FROM pegawai);
You did it correctly when you were setting currDate and birthdate, but you forgot the parentheses on this line.