MySQL problem using CREATE FUNCTION in cPanel - mysql

I have been receiving always an error while creating mysql function in cpanel with below code.
But the below code is running properly while creating mysql function in SQL Manger 2007.
Anyone tell me what is wrong with this code for cpanel.
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 '' at line 12
(0.81 sec)
CREATE FUNCTION F_test (topsbwynum INTEGER)
RETURNS float(20,2)
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
BEGIN
DECLARE FinalPrice VARCHAR(20);
DECLARE err VARCHAR(20);
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET err = 1;
IF(sbwynum > 0)THEN
IF(FinalPrice > 0)THEN
RETURN FinalPrice;
ELSEIF(adjprice > 0)THEN
RETURN adjprice;
ELSEIF(price > 0)THEN
RETURN price;
ELSEIF(estprice > 0)THEN
RETURN estprice;
ELSE
RETURN 0;
END IF;
ELSE
RETURN 0;
END IF;
END;
Thanks a lot.

For multi-line statements, you can't use the same character (";", by default) to delimit the individual statements within the function definition and the entire CREATE statement. If you try, the creation statement ends at the first ";" (in the sample code, it's after the first DECLARE). Since the statement syntax error is at the end of the truncated statement, the error message gives '' as the part of the statement where the error occurs.
From the command line client, you'd use the delimiter command to change the delimiter used by the client to (e.g.) ";;", then end the creation statement with this delimiter:
delimiter ;;
CREATE FUNCTION F_test (topsbwynum INTEGER)
...
END;;
delimiter ;
In phpMyAdmin, there's a "delimiter" field that fulfills the same purpose.

Related

"No Return Found" error in simple mysql function

I'm using xampp with phpMyAdmin to manage a MySql database.
When creating a function I got a "No Return Found" error, so I stripped down the function to narrow the origin of the error I got to the simplest case where it still doesn't work.
And the error message is this:
Apparently if the RETURN statement isn't the only thing on the code I get an error.
Assuming the actual code being executed against MySQL is:
CREATE FUNCTION `Contagem`() RETURNS INT(11)
SET #c = 0;
RETURN #c;
This will fail, because you have not wrapped the function in BEGIN...END.
Try instead to write this as your function declaration:
BEGIN
SET #c = 0;
RETURN #c;
END;
Alternatively, declare the function yourself directly in MySQL:
DELIMITER $$
CREATE FUNCTION `Contagem`() RETURNS INT(11)
BEGIN
SET #c = 0;
RETURN #c;
END $$
DELIMITER ;
When reviewing this post I was reading more carefully the error message I realized the MySql code to define the function didn't have BEGIN and END in it.
I assumed when typing function code with several lines that phpMyAdmin would know how to handle it, since the code text box has support to multiple lines of code.
So, putting BEGIN at the beginning and END at the end of the code solved my problem.

Loop syntax error in MySQL cursor

I have been trying to use a cursor and following a tutorial I found online, I was able to come up with the following cursor.
DELIMITER $$
CREATE PROCEDURE customers_with_oldest_version (INOUT customerCount varchar(4000))
BEGIN
DEClARE customers_with_oldest_version CURSOR FOR
select * from CustomerSoftware where software in (select min(minimumSoftware) from ProductSoftware);
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET #finished = 1;
set #row_entry = "";
open customers_with_oldest_version;
get_customers: LOOP
FETCH customers_with_oldest_version INTO #row_entry;
IF #finished = 1 THEN
LEAVE get_customers;
END IF;
SET #customerCount = #customerCount + 1;
END LOOP;
CLOSE customers_with_oldest_version;
END$$
DELIMITER ;
But I am unable to create this procedure, since phpmyadmin is giving me an error saying that
#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 '#row_entry; IF #finished = 1 THEN LEAVE get_customers; END IF; SET #c' at line 16
What am I missing here?
You can't fetch into a user-defined variable. This is a bug that has been around for many years, see https://bugs.mysql.com/bug.php?id=2261
Declare a regular local variable for it.

Error on Creating Stored Function in MySQL 5.0.5

I'm usually working with mssql or postgres, but I need to use MySql in the current project.
Now I have a problem with creating a stored function in MySQL 5.0.5.
I'm getting the following 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 4
My Code looks like that:
CREATE FUNCTION aip_sf_choice_valid (request_id INT)
RETURNS INT
BEGIN
DECLARE sf_result INT;
SET sf_result = 1;
RETURN sf_result
END
I'm really out of ideas. I'm glad for any help!
You need to change the default delimiter from ';' within the SP body. More info on that here: http://dev.mysql.com/doc/refman/5.1/en/create-procedure.html
Try this:
DELIMITER $$
CREATE FUNCTION aip_sf_choice_valid (request_id INT)
RETURNS INT
BEGIN
DECLARE sf_result INT;
SET sf_result = 1;
RETURN sf_result;
END
Should it have a semicolon after "END"?

Syntax error of ''?

I can't seem to figure out where this is coming from... MySQL give me a syntax error of an empty quote and gives me a line number that doesn't seem to be wrong at all. Worse still, deleting the loop the line number points to still gives me the same error, just with a different line number.
ERROR 1064 (42000) at line 13: 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 39
Talk about unhelpful feedback from MySQL!
The code in question is a stored function, and I ran into this when trying to apply the answer to another question. The updated code is available here.
EDIT: #MarkByers, here's the function reduced as low as I could get it while still triggering the error:
DROP FUNCTION IF EXISTS months_within_range;
DELIMITER //
CREATE FUNCTION months_within_range(starts_at DATE, ends_at DATE, filter_range VARCHAR(255)) RETURNS TINYINT
BEGIN
SET #matches = 1;
IF #matches >= 1 THEN RETURN 1;
ELSE RETURN 0;
END//
DELIMITER ;
You are missing the END IF
IF #matches >= 1 THEN RETURN 1;
ELSE RETURN 0;
END IF;
And the reason RETURN IF(#matches >= 1, 1, 0); works is because this is the IF function, which is different from the IF statement

MySQL 5.1.39 SQL syntax error

I have made an upgrade for my MySQL Server to 5.1.39 and now when I run SQL scripts (which had worked previously) - it throws error. I have checked syntax many times and I couldn't find any incompatible code parts. Please suggest any solution for this problem.
Error message
Mysql::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 clean_dymmy_table (dummy_name VARCHAR(255)) RETURNS V' at line 3:
SQL code:
/*DELIMITER //*/
DROP FUNCTION IF EXISTS clean_dymmy_table;
CREATE FUNCTION clean_dymmy_table (dummy_name VARCHAR(255)) RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE temp_val VARCHAR(255);
SET temp_val = dummy_name;
-- Test
SET temp_val = REPLACE(temp_val, 'Tmp ', '');
SET temp_val = REPLACE(temp_val, ' TmP', '');
SET temp_val = REPLACE(temp_val, 'TMP ', '');
SET temp_val = REPLACE(temp_val, ' TMP', '');
SET temp_val = REPLACE(temp_val, ' tmp', '');
RETURN dummy_name;
END/*//*/
Not sure why you removed the DELIMITER part, but when I add that back in, it runs fine:
DELIMITER // -- you have to change what MySQL expects between commands
DROP FUNCTION IF EXISTS clean_dymmy_table // -- tell it a new command's coming
CREATE FUNCTION clean_dymmy_table (dummy_name VARCHAR(255))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
-- now this can be parsed as part of the current command.
DECLARE temp_val VARCHAR(255);
SET temp_val = dummy_name;
-- Test
RETURN dummy_name;
END
// -- Now you're done with that command.
-- go back to semi-colons, because otherwise life is too zany for me.
DELIMITER ;
(This was in 5.1.54... but I don't think that should matter)
A hunch: There seems to be a MySQL Bug in the 5.1.x line pertaining to DELIMITER that may be biting you here:
#46429 use DELIMITER command in MySql.Data.MySqlClient.MySqlScript
Though that heavily depends on how you're calling it, given cwallenpoole's response, your symptom does suggest it may well be that bug, given your MySQL version. Is upgrading a possibility for you?