delimiter |
CREATE FUNCTION BASE64_DECODE (input BLOB)
RETURNS BLOB
CONTAINS SQL
DETERMINISTIC
SQL SECURITY INVOKER
BEGIN
DECLARE ret BLOB DEFAULT '';
DECLARE done TINYINT DEFAULT 0;
IF input IS NULL THEN
RETURN NULL;
END IF;
each_block:
WHILE NOT done DO BEGIN
DECLARE accum_value BIGINT UNSIGNED DEFAULT 0;
DECLARE in_count TINYINT DEFAULT 0;
DECLARE out_count TINYINT DEFAULT 3;
each_input_char:
WHILE in_count < 4 DO BEGIN
DECLARE first_char CHAR(1);
IF LENGTH(input) = 0 THEN
RETURN ret;
END IF;
SET first_char = SUBSTRING(input,1,1);
SET input = SUBSTRING(input,2);
BEGIN
DECLARE tempval TINYINT UNSIGNED;
DECLARE error TINYINT DEFAULT 0;
DECLARE base64_getval CURSOR FOR SELECT val FROM base64_data WHERE c = first_char;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET error = 1;
OPEN base64_getval;
FETCH base64_getval INTO tempval;
CLOSE base64_getval;
IF error THEN
ITERATE each_input_char;
END IF;
SET accum_value = (accum_value << 6) + tempval;
END;
SET in_count = in_count + 1;
IF first_char = '=' THEN
SET done = 1;
SET out_count = out_count – 1;
END IF;
END; END WHILE;
WHILE out_count > 0 DO BEGIN
SET ret = CONCAT(ret,CHAR((accum_value & 0xff0000) >> 16));
SET out_count = out_count – 1;
SET accum_value = (accum_value << 8) & 0xffffff;
END; END WHILE;
END; END WHILE;
RETURN ret;
END |
The error I'm getting is:
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 '– 1;
END IF;
END; END WHILE;
' at line 52
WHILE loops in MySQL are simply terminated with END WHILE;. You've got END; END WHILE; which is incorrect.
That END before END WHILE; is causing the problem. You already have an END on line 46, so this one is trying to end the function. I'll bet you can just remove it.
The problem, silly as it was, was the use of non-standard symbols throughout. -s and 's were wrong format. Due to the encoding of the IDE I am using or something just as silly.
facepalm
Related
I'm on developing web project and i get some problem with migration from oracle database to mysql database. I want to create function with this code :
DROP FUNCTION IF EXISTS F_MANIFEST_GABUNG_SMR;
DELIMITER //
CREATE FUNCTION F_MANIFEST_GABUNG_SMR (input_val varchar(4000))
RETURNS VARCHAR(4000)
BEGIN
DECLARE return_text VARCHAR(10000) DEFAULT NULL;
DECLARE not_found INT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET not_found = 1;
DECLARE x CURSOR FOR SELECT DISTINCT IFNULL(SMR,'-') SMR FROM MANIFEST_EDI_SMR WHERE BL_NBR = input_val; OPEN x;
FETCH x INTO;
WHILE NOT_FOUND=0
DO
SET return_text = concat(ifnull(return_text, '') , ' ' , IFNULL(x.SMR, '')) ;
FETCH INTO;
END WHILE;
CLOSE ;
IF char_length(return_text) > 85 THEN
SET return_text = concat(ifnull(substr(return_text,1,85), '') , ' detail asp BL');
END IF;
RETURN return_text;
END;
//
DELIMITER ;
I am using phpmyadmin to store function with routine. Thanks for your help :)
The error message is self explanatory:you have declare the cursor after handler,need to change the order:
DROP FUNCTION IF EXISTS F_MANIFEST_GABUNG_SMR;
DELIMITER //
CREATE FUNCTION F_MANIFEST_GABUNG_SMR (input_val varchar(4000))
RETURNS VARCHAR(4000)
BEGIN
DECLARE return_text VARCHAR(10000) DEFAULT NULL;
DECLARE not_found INT DEFAULT 0;
-- Declare cursor before handler
DECLARE x CURSOR FOR SELECT DISTINCT IFNULL(SMR,'-') SMR
FROM MANIFEST_EDI_SMR WHERE BL_NBR = input_val; OPEN x;
-- handler need to be after cursor
DECLARE CONTINUE HANDLER FOR NOT FOUND SET not_found = 1;
FETCH x INTO;
WHILE NOT_FOUND=0
DO
SET return_text = concat(ifnull(return_text, '') , ' ' , IFNULL(x.SMR, '')) ;
FETCH INTO;
END WHILE;
CLOSE ;
IF char_length(return_text) > 85 THEN
SET return_text = concat(ifnull(substr(return_text,1,85), '') , ' detail asp BL');
END IF;
RETURN return_text;
END;
//
DELIMITER ;
More details can be found at:https://dev.mysql.com/doc/refman/8.0/en/cursors.html
My question says everything. I have mysql stored function with return type decimal 12,2.
This is the whole code:
BEGIN
-- The only edit is ony line number 9, 32 and 33
DECLARE ctrNumber VARCHAR(50);
DECLARE finNumber VARCHAR(50) DEFAULT '';
DECLARE sChar VARCHAR(1); -- be aware varchar length is 1
DECLARE inti INTEGER DEFAULT 1;
SET #in_string = '.abc12';
IF LENGTH(#in_string) > 0 THEN
-- Loops from . to 1 (.abc1)
WHILE(inti <= LENGTH(#in_string)) DO
SET sChar = SUBSTRING(#in_string, inti, 1);
-- Find if it contains the following array
SET ctrNumber = FIND_IN_SET(sChar, '0,1,2,3,4,5,6,7,8,9,.');
-- If returns true or 1 or 2 or positive number it means it contains
IF ctrNumber > 0 THEN
SET finNumber = CONCAT(finNumber, sChar);
END IF;
SET inti = inti + 1;
END WHILE;
-- end of loop from . to 1
RETURN CAST(finNumber AS DECIMAL(12,2)) AS final;
ELSE
RETURN 0;
END IF;
END
the error happens in the part:
RETURN CAST(finNumber AS DECIMAL(12,2)) AS final;
The finNumber is ".12" I convert it from string to decimal.
When I tried it on Stored procedure, using SELECT CAST(finNumber AS DECIMAL(12,2)) AS final; it works fine.
You can copy my whole code with return type of decimal 12,2 and paste it in stored function/procedure.
I have tried it in both MySQL and MariaDB, it doesn't look like that's the case.
This is the error:
The following query has failed: "CREATE FUNCTION ExtractNumber()
RETURNS DECIMAL(10,2) NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER
BEGIN -- The only edit is ony line number 9, 32 and 33 DECLARE
ctrNumber VARCHAR(50); DECLARE finNumber VARCHAR(50) DEFAULT '';
DECLARE sChar VARCHAR(1); -- be aware varchar length is 1 DECLARE inti
INTEGER DEFAULT 1; SET #in_string = '.abc12'; IF LENGTH(#in_string) >
0 THEN -- Loops from . to 1 (.abc1) WHILE(inti <= LENGTH(#in_string))
DO SET sChar = SUBSTRING(#in_string, inti, 1); -- Find if it contains
the following array SET ctrNumber = FIND_IN_SET(sChar,
'0,1,2,3,4,5,6,7,8,9,.'); -- If returns true or 1 or 2 or positive
number it means it contains IF ctrNumber > 0 THEN SET finNumber =
CONCAT(finNumber, sChar); END IF; SET inti = inti + 1; END WHILE; --
end of loop from . to 1 -- original code: remove CAST and UNSIGNED --
UNSIGNED removes all special chars or letters -- because CAST converts
number into INT or UNSIGNED -- RETURN CAST(finNumber AS UNSIGNED) AS
final; RETURN CAST(finNumber AS DECIMAL(12,2)) AS final; ELSE RETURN
0; END IF; END "
MySQL said: #1064 - 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 'AS final; ELSE RETURN 0; END IF; END' at line 33
We are using the following stored procedure and all the mentioned tables are using "Collation = utf8_general_ci" Still we are getting this error:
Error Code: 1267. Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation '='
Store Procedure is:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `AssignCallRates`()
begin
declare countrycode varchar(8000);
declare countryname varchar(8000);
declare currencycode varchar(8000);
declare priceval varchar(8000);
declare mobileprice varchar(8000);
declare landprice varchar(8000);
declare reccnt int;
DECLARE done INT DEFAULT FALSE;
declare country_cursor cursor for select country_code,country_name from dialoone_countries;
declare aud_cursor cursor for select convert(price,char) as price from tbl_rate_aud where quick_search=1 and trim(substring_index(`place`,'-',1)) = countryname LIMIT 0,2;
declare euro_cursor cursor for select convert(price,char) as price from tbl_rate_euro where quick_search=1 and trim(substring_index(`place`,'-',1)) = countryname LIMIT 0,2;
declare gbp_cursor cursor for select convert(price,char) as price from tbl_rate_gbp where quick_search=1 and trim(substring_index(`place`,'-',1)) = countryname LIMIT 0,2;
declare usd_cursor cursor for select convert(price,char) as price from tbl_rate_dollar where quick_search=1 and trim(substring_index(`place`,'-',1)) = countryname LIMIT 0,2;
declare continue handler for not found set done=TRUE;
truncate table tbl_rates;
open country_cursor;
CountryLOOP: loop
fetch country_cursor into countrycode,countryname;
if done=TRUE then
close country_cursor;
leave CountryLOOP;
end if;
set mobileprice = "";
set landprice="";
set reccnt = 0;
set priceval = "";
open aud_cursor;
AUDLOOP: loop
fetch aud_cursor into priceval;
if done = TRUE then
set done = FALSE;
close aud_cursor;
leave AUDLOOP;
end if;
set reccnt = reccnt + 1;
if reccnt = 1 then
set landprice=priceval;
end if;
if reccnt = 2 then
set mobileprice=priceval;
end if;
end loop AUDLOOP;
insert into tbl_rates (country_code,currency_code,mobile,land) values (countrycode,"AUD",mobileprice,landprice);
set mobileprice = "";
set landprice="";
set reccnt = 0;
set priceval = "";
open euro_cursor;
EUROLOOP: loop
fetch euro_cursor into priceval;
if done = TRUE then
set done = FALSE;
close euro_cursor;
leave EUROLOOP;
end if;
set reccnt = reccnt + 1;
if reccnt = 1 then
set landprice=priceval;
end if;
if reccnt = 2 then
set mobileprice=priceval;
end if;
end loop EUROLOOP;
insert into tbl_rates (country_code,currency_code,mobile,land) values (countrycode,"EUR",mobileprice,landprice);
set mobileprice = "";
set landprice="";
set reccnt = 0;
set priceval = "";
open gbp_cursor;
GBPLOOP: loop
fetch gbp_cursor into priceval;
if done = TRUE then
set done = FALSE;
close gbp_cursor;
leave GBPLOOP;
end if;
set reccnt = reccnt + 1;
if reccnt = 1 then
set landprice=priceval;
end if;
if reccnt = 2 then
set mobileprice=priceval;
end if;
end loop GBPLOOP;
insert into tbl_rates (country_code,currency_code,mobile,land) values (countrycode,"GBP",mobileprice,landprice);
set mobileprice = "";
set landprice="";
set reccnt = 0;
set priceval = "";
open usd_cursor;
USDLOOP: loop
fetch usd_cursor into priceval;
if done = TRUE then
set done = FALSE;
close usd_cursor;
leave USDLOOP;
end if;
set reccnt = reccnt + 1;
if reccnt = 1 then
set landprice=priceval;
end if;
if reccnt = 2 then
set mobileprice=priceval;
end if;
end loop USDLOOP;
insert into tbl_rates (country_code,currency_code,mobile,land) values (countrycode,"USD",mobileprice,landprice);
end loop CountryLOOP;
select "Query Executed Successfully";
end$$
DELIMITER ;
Any updation needed to this SP?
From other answer: The default collation for stored procedure parameters is utf8_general_ci and you can't mix collations, so that means either database / tables are probably unicode.
First Try this:
SELECT ##collation_database;
Then look into the CREATE statement for the actual tables you are using.
More Info
i want to split my row something like this:
example:
id data
1 23#4#
2 2#4#5#
to be like this
id data
1 23
1 4
2 2
2 4
2 5
i have looking for this problem in stack overflow but the solution is not satisfaction me. please help me.
i try to make a store procedure in mysql, the code it's ok no error, but the result is not ok.
drop procedure if exists pecah_lokasi_sp;
create procedure pecah_lokasi_sp()
begin
DECLARE ii INT;
DECLARE ki varchar(255);
DECLARE ulang int DEFAULT 1;
DECLARE hasil varchar(255) DEFAULT '';
DECLARE selesai INT DEFAULT FALSE;
DECLARE kursor CURSOR FOR
SELECT id_iklan, lokasi_kerja_utama
FROM iklan_lowongan_tb
WHERE status = 'Aktif';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET selesai = TRUE;
DELETE FROM temp_lok_tb;
OPEN kursor;
read_loop: LOOP
FETCH kursor INTO ii, ki;
IF selesai THEN
LEAVE read_loop;
END IF;
WHILE (ulang < LENGTH(ki)) DO
IF SUBSTRING(ki, ulang, 1) <> '#' THEN
SET hasil = CONCAT(hasil, SUBSTRING(ki, ulang, 1));
ELSE
INSERT INTO temp_lok_tb (id_iklan, id_kota) VALUES(ii, hasil);
SET hasil = '';
END IF;
SET ulang = ulang+1;
END WHILE;
SET ulang = 1;
END LOOP read_loop;
CLOSE kursor;
select * from temp_lok_tb;
end;
i think i can solve my problem myself.
this is my revision code, the result is true.
drop procedure if exists pecah_lokasi_sp;
create procedure pecah_lokasi_sp()
begin
DECLARE ii INT;
DECLARE ki varchar(255);
DECLARE ulang int DEFAULT 1;
DECLARE hasil varchar(255) DEFAULT '';
DECLARE selesai INT DEFAULT FALSE;
DECLARE jmlkarakter int;
DECLARE kursor CURSOR FOR
SELECT id_iklan, lokasi_kerja_utama
FROM iklan_lowongan_tb
WHERE status = 'Aktif';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET selesai = TRUE;
DELETE FROM temp_lok_tb;
OPEN kursor;
read_loop: LOOP
FETCH kursor INTO ii, ki;
IF selesai THEN
LEAVE read_loop;
END IF;
SET jmlkarakter = LENGTH(ki) - LENGTH(REPLACE(ki, '#', '')) ;
WHILE (ulang <= jmlkarakter) DO
IF ulang = 1 THEN
INSERT INTO temp_lok_tb(id_iklan, id_kota) VALUES(ii, SUBSTRING_INDEX(ki,'#',1));
ELSE
INSERT INTO temp_lok_tb(id_iklan, id_kota) VALUES(ii, SUBSTRING_INDEX(SUBSTRING_INDEX(ki,'#', ulang),'#',-1));
END IF;
SET ulang = ulang+1;
END WHILE;
SET ulang = 1;
END LOOP read_loop;
CLOSE kursor;
select * from temp_lok_tb;
end;
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