Unable to create UDF in my sql - mysql

Unable to create UDF. Am getting below error at the time of creation
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 ';
CREATE FUNCTION `seat_count`(seatno varchar(250))
RETURNs varchar(250)
BEG' at line 1
code: http://pastebin.com/uf4DkXZh
code:
CREATE FUNCTION `seat_count`(seatno VARCHAR(250))
RETURNS VARCHAR(250)
BEGIN
SET #str= REPLACE(seatno, ',', '');
SET #stcnt=CHAR_LENGTH(str);
SET #x=1;
SET #result;
WHILE (#x <= #stcnt)
DO
SET #st=SUBSTRING_INDEX(#str,',',X);
IF ((LENGTH(#seatno) - LENGTH(REPLACE(#seatno, st, '')))/LENGTH(#st)=1)
THEN SET #result=CONCAT(#st,#result);
END IF;
SET #x=#x+1;
END WHILE;
RETURN #result;
END$$

When you develop a stored procedure in MySQL, you can pass the input parameters and declare the local variables, the local variable are not prepended with any prefixes unlike user-defined variable which are prepended with #. For detail procedure vs user-defined variable
DELIMITER $$
CREATE FUNCTION `SEAT_COUNT`(`seatno` VARCHAR(250)) RETURNS VARCHAR(250)
DETERMINISTIC
BEGIN
DECLARE result VARCHAR(250);
SET #str = REPLACE(seatno, ',', '');
SET #stcnt = CHAR_LENGTH(#str);
SET #x = 1;
WHILE (#x <= #stcnt)
DO
SET #st = SUBSTRING_INDEX(#str,',',X);
IF ((LENGTH(#seatno) - LENGTH(REPLACE(#seatno, st, '')))/LENGTH(#st)=1)
THEN SET result = CONCAT(#st, #result);
END IF;
SET #x = #x+1;
END WHILE;
RETURN result;
END$$
DELIMITER ;
You have defined the result variable as user-defined e.g SET #result instead of local variable e.g DECLARE result VARCHAR(250)
I hope this will work at your end too.

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 Syntax error while creating stored function

I am getting a syntax error on executing the below function in MySQL.
CREATE FUNCTION FABC (P_MEMBER_EMAIL VARCHAR(30))
RETURNS int
BEGIN
DECLARE RESULT int;
DECLARE V_DATE DATE;
SELECT DOJ+365 INTO V_DATE FROM CHYK_MEMBER_MASTER
WHERE UPPER(MEMBER_EMAIL)=UPPER(P_MEMBER_EMAIL) AND SUBSCRIPTION='Y';
IF V_DATE>SYSDATE THEN
SET RESULT=1;
ELSE
SET RESULT=0;
END IF;
RETURN RESULT;
END;
The error is as follows "#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 4
Not sure what's causing this. Can anyone help me ?
You need to set a delimiter that will tell MySQL the END of the function definition.
DELIMITER $$
CREATE FUNCTION `FABC`(`P_MEMBER_EMAIL` VARCHAR(30)) RETURNS int(11)
BEGIN
DECLARE RESULT int;
DECLARE V_DATE DATE;
SELECT DOJ+365 INTO V_DATE FROM CHYK_MEMBER_MASTER
WHERE UPPER(MEMBER_EMAIL)=UPPER(P_MEMBER_EMAIL) AND SUBSCRIPTION='Y';
IF V_DATE>SYSDATE THEN
SET RESULT=1;
ELSE
SET RESULT=0;
END IF;
RETURN RESULT;
END$$
DELIMITER ;

How to check output of function or variable for NULL

I am bit confused with Mysql syntax. I want to check for NULL the value of ExtractValue(xml, '//order[1]/quantity[$#i]') function. It can be assign to variable or this action can be skipped. I tried this and there is syntax error:
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_test_for_null`$$
CREATE PROCEDURE `sp_test_for_null`()
BEGIN
DECLARE xml VARCHAR(1000);
SET xml = '';
DECLARE test VARCHAR(1000);
SET test = (SELECT ExtractValue(xml, '//order[1]/quantity[$#i]');
IF (test IS NULL) THEN SELECT 1; END IF;
END$$
DELIMITER ;
CALL sp_test_for_null;
I'd try this (note that all DECLAREs are at the beginning:
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_test_for_null`$$
CREATE PROCEDURE `sp_test_for_null`()
BEGIN
DECLARE xml VARCHAR(1000);
DECLARE test VARCHAR(1000);
SET xml = '';
SET test = (SELECT ExtractValue(xml, '//order[1]/quantity[$#i]');
SELECT ISNULL(test, 1, 0);
END$$
DELIMITER ;
CALL sp_test_for_null;

Mysql - How to split text variable in stored procedure

I have a MySQL variable as below.
DECLARE str TEXT DEFAULT '2014-01-02 13:00:00|2014-02-04 12:59:59#0#2014-02-04 13:00:00|2014-03-04 12:59:59#0#2014-03-04 13:00:00|2014-04-02 13:59:59#0#2014-04-02 14:00:00|2014-05-02 14:59:59#0#2014-05-02 15:00:00|2014-06-03 14:59:59';
I want to break this whole string first by using the separator #0# and from the results break the string using separator |.
I have tried MySQL split_str function but I am not able to do it.
Its giving me the error split_str does not exist.
Please suggest some other way to do this.
Finally i have resolved my problem using below procedure.
I am able to solve it using temporary table and procedure. I am no more using mysql variable for this. We can call the procedure as below....
CALL SplitString('2014-01-02 13:00:00|2014-02-04 12:59:59#0#2014-02-04 13:00:00|2014-03-04 12:59:59#0#2014-03-04 13:00:00|2014-04-02 13:59:59#0#2014-04-02 14:00:00|2014-05-02 14:59:59#0#2014-05-02 15:00:00|2014-06-03 14:59:59', '#0#', 'tblindex');
Here tblindex is temporary table i have used.
My procedure is below....
DELIMITER $$
DROP PROCEDURE IF EXISTS `SplitString`$$
CREATE PROCEDURE `SplitString`( IN input TEXT,IN delm VARCHAR(10),tblnm varchar(50))
BEGIN
DECLARE cur_position INT DEFAULT 1 ;
DECLARE remainder TEXT;
DECLARE cur_string VARCHAR(1000);
DECLARE delm_length TINYINT UNSIGNED;
set #sql_drop = concat("DROP TABLE IF EXISTS ",tblnm);
prepare st_drop from #sql_drop;
execute st_drop;
set #sql_create = concat("CREATE TEMPORARY TABLE ",tblnm," (value VARCHAR(2000) NOT NULL ) ENGINE=MEMORY;");
prepare st_create from #sql_create;
execute st_create;
SET remainder = input;
SET delm_length = CHAR_LENGTH(delm);
WHILE CHAR_LENGTH(remainder) > 0 AND cur_position > 0
DO
SET cur_position = INSTR(remainder, delm);
IF cur_position = 0 THEN
SET cur_string = remainder;
ELSE
SET cur_string = LEFT(remainder, cur_position - 1);
END IF;
-- select cur_string;
IF TRIM(cur_string) != '' THEN
set #sql_insert = concat("INSERT INTO ",tblnm," VALUES ('",cur_string,"');");
prepare st_insert from #sql_insert;
execute st_insert;
END IF;
SET remainder = SUBSTRING(remainder, cur_position + delm_length);
END WHILE;
END$$
DELIMITER ;

MySQL - Trouble with creating user defined function (UDF)

I'm trying to create this function:
CREATE FUNCTION remove_non_alphanum (prm_strInput varchar(3000))
RETURNS VARCHAR(3000)
DETERMINISTIC
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE v_char VARCHAR(1);
DECLARE v_parseStr VARCHAR(3000) DEFAULT '';
WHILE (i <= LENGTH(prm_strInput) ) DO
SET v_char = SUBSTR(prm_strInput,i,1);
IF v_char REGEXP '^[A-Za-z0-9]$' THEN
SET v_parseStr = CONCAT(v_parseStr,v_char);
END IF;
SET i = i + 1;
END WHILE;
RETURN trim(v_parseStr);
END
But MySQL says:
13:52:45 [CREATE - 0 row(s), 0.000 secs] [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 '' at line 5
What could I being wrong? The syntax looks correct to me.
You have to change the delimiter so you can use ; inside the function:
DELIMITER $$
CREATE FUNCTION remove_non_alphanum (prm_strInput varchar(3000))
RETURNS VARCHAR(3000)
DETERMINISTIC
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE v_char VARCHAR(1);
DECLARE v_parseStr VARCHAR(3000) DEFAULT '';
WHILE (i <= LENGTH(prm_strInput) ) DO
SET v_char = SUBSTR(prm_strInput,i,1);
IF v_char REGEXP '^[A-Za-z0-9]$' THEN
SET v_parseStr = CONCAT(v_parseStr,v_char);
END IF;
SET i = i + 1;
END WHILE;
RETURN trim(v_parseStr);
END
$$
DELIMITER ;
In MySQL Command-Line Client (and many other SQL clients) the default delimiter is ;. So, when you type your original code, MySQL thinks the first command ends where the first ; is found (at line 5, as the error message states), thus you get an error because this is not valid SQL:
CREATE FUNCTION remove_non_alphanum (prm_strInput varchar(3000))
RETURNS VARCHAR(3000)
DETERMINISTIC
BEGIN
DECLARE i INT DEFAULT 1;
If you change the delimiter to anything else, MySQL identifies the complete command (from CREATE FUNCTION to END and runs it. Voilá! Your function is created. Finally, when you run your function, the code runs just fine because the function body is composed of several statements using the default delimiter.
I found the answer here.
I turns out it was some weird DB Visualizer issue.
Enclosing the complete block in "--/" and "/" worked for me:
--/
CREATE FUNCTION remove_non_alphanum (prm_strInput varchar(3000))
RETURNS VARCHAR(3000)
DETERMINISTIC
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE v_char VARCHAR(1);
DECLARE v_parseStr VARCHAR(3000) DEFAULT '';
WHILE (i <= LENGTH(prm_strInput) ) DO
SET v_char = SUBSTR(prm_strInput,i,1);
IF v_char REGEXP '^[A-Za-z0-9]$' THEN
SET v_parseStr = CONCAT(v_parseStr,v_char);
END IF;
SET i = i + 1;
END WHILE;
RETURN trim(v_parseStr);
END
/
An alternative to
--/
CREATE FUNCTION ...
/
is:
#delimiter $$;
CREATE FUNCTION ...
#delimiter ;$$
More info: http://www.dbvis.com/doc/main/doc/ug/sqlCommander/sqlCommander.html#mozTocId437790