What is the correct version of this sql function? - mysql

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 ;

Related

Declare inside if condition showing syntax error - MYSQL Triggers

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.

MySQL script inside set statement

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

What's wrong w/ MySql Statement (Function)

I'm using MySql57
What's wrong with this script?(I'm Mysql newbie)
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 '= 'A0001'; ELSE BEGIN gubun1 = LEFT(cur_max,0,1); gu' at line 10
DELIMITER $$
CREATE FUNCTION narae.FN_GET_GUBUN_MAX() RETURNS varchar(100)
BEGIN
DECLARE cur_max varchar(100);
DECLARE gubun1 varchar(1);
DECLARE gubun2 varchar(100);
DECLARE RTN_VAL varchar(100);
SELECT IFNULL(MAX(gubun_code),'A0001') INTO cur_max from gubun_code;
IF cur_max = 'A0001' THEN RTN_VAL = 'A0001';
ELSE
BEGIN
gubun1 = LEFT(cur_max,0,1);
gubun2 =
LPAD(CONVERT(CONVERT(RIGHT(cur_max,4),UNSIGNED)+1,CHAR),4,'0');
RTN_VAL = CONCAT(gubun1,gubun2);
END
RETURN RTN_VAL;
END $$
DELIMITER ;
There are several errors in the function. I think this might work:
DELIMITER $$
CREATE FUNCTION narae.FN_GET_GUBUN_MAX() RETURNS varchar(100)
BEGIN
DECLARE v_cur_max varchar(100);
DECLARE v_RTN_VAL varchar(100);
SELECT COALESCE(MAX(gubun_code), 'A0001')
INTO v_cur_max
FROM gubun_code;
IF cur_max = 'A0001' THEN
SET v_RTN_VAL = 'A0001';
ELSE
SET v_RTN_VAL = CONCAT(LEFT(cur_max, 1), RIGHT(cur_max, 4) + 1);
END IF;
RETURN v_RTN_VAL;
END $$
DELIMITER ;
I would further simplify this by doing all the logic in the SQL statement, but this seems to be in the spirit of your solution.

Error Code : 1064 You have an error in your SQL syntax (the new One)

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.

Getting a MYSQL 1064 error when creating a function

DELIMITER $$
CREATE FUNCTION nameOfFunct(intIn int)
RETURN int
BEGIN
DECLARE intOut INT;
SET intOut = SELECT count(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn;
RETURN intOut;
END;
$$
DELIMITER;
If I try to run this all I get 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
'RETURN int
BEGIN
DECLARE intOut INT;
SET intOut = select count(' at line 2
A couple of changes to resolve the problem:
DELIMITER $$
CREATE FUNCTION nameOfFunct(intIn INT)
-- RETURN INT
RETURNS INT
BEGIN
DECLARE intOut INT;
/*SET intOut = SELECT COUNT(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn;*/
SET intOut = (SELECT COUNT(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn);
RETURN intOut;
END $$
DELIMITER ;
Defining the return type is done by the RETURNS keywrod, not the RETURN keyword:
CREATE FUNCTION nameOfFunct(intIn int)
RETURNS int
BEGIN
DECLARE intOut INT;
SET intOut = SELECT count(*)
FROM tableToTakeFrom
WHERE columToCompareTo = intIn;
RETURN intOut;
END;